views:

310

answers:

3

I have an MVC page, with some controls inside a form. The part I need help with: I have a bunch of dropdowns in a list. All dynamically named (drop{0}, where {0} is the id (really, its just a counter: 1,2,3,etc)). At the top of the list, I want to have another dropdown that will update ALL the dropdowns when it is changed. I've done similar things with checkboxes (check one and all are checked, etc) so I assume this can be done, hopefully just as simple. I'd prefer it to be on the client side, so once the form is submitted, the new values will be added/updated to the database.

Edit: The values of ALL the dropdowns are static. They are all a list of 1-50, representing the number of cards I need to produce for a given record.

This is how I did the checkbox:

$("#chkSelectAll").click(function() {
      $(".checkbox").attr('checked', this.checked);
 });

Any thoughts on where to begin?

Thanks!

+1  A: 

You could use the starts with selector:

$('#somedropdown').change(function() {
    // when the value of the dropdown changes loop through other dropdowns
    // whose id begins with "drop"
    $('select[id^=drop]').each(function() {
        // do something with the dropdown
    });
 });
Darin Dimitrov
This is going to sound dumb, but what is the "something" I do?! For example, for the checkbox, I do $(".checkbox").attr('checked', this.checked);which changes all checkboxes with the class .checkbox to checked. I just dont know what the attributes are of the dropdown for jquery, and cant find any documentation on it...
SlackerCoder
You still haven't stated in your question what you want to do with these dropdowns that's why I left the comment. For example for a checkbox you could set its checked state, a dropdown you could change the selected value, ...
Darin Dimitrov
Gotcha, I actually didnt want them to do anything, just display the change.The "something" I needed was the .attr('selectedIndex', ...). I couldn't figure out what was needed for the .attr (I tried 'selected' but that wasnt it...someone here tipped me off with 'selectedIndex')
SlackerCoder
A: 

I understand that you want to set the selection of the other dropdowns, using jQuery. If so, do this....

function onSelectChange(){
    var stext = $("#dropdown0 option:selected").val();
    switch (stext) {
    case "Value1":
        $("#dropdown1 > option[value='Good']").attr('selected','selected');
        $("#dropdown2 > option[value='9000']").attr('selected','selected');
        break;
    case "Value2":
        $("#dropdown1 > option[value='Better']").attr('selected','selected');
        $("#dropdown2 > option[value='34000']").attr('selected','selected');
        break;
    case "Value3":
        $("#dropdown1 > option[value='Good']").attr('selected','selected');
        $("#dropdown2 > option[value='1000']").attr('selected','selected');
        break;
    case "Value4":
        $("#dropdown1 > option[value='Better']").attr('selected','selected');
        $("#dropdown2 > option[value='9000']").attr('selected','selected');
    }
}

working example: http://jsbin.com/odabe

Cheeso
A: 

One possible solution (of apparently many):

        $("#ddlQuantity").change(function() {
            $(".quantity").attr('selectedIndex', this.selectedIndex);
        });

Since the .quantity class applies to all the created objects, changing the .attr of the "class" worked just fine.

SlackerCoder