views:

114

answers:

2

I have some dropdowns shown in my asp.net mvc application with the same name (say: 5 dropdowns with the same name 'uniquedropdown'.)

I need to get all the selected values of the dropdowns with the same name using jquery.

How to get it?

A: 

Use the each function to iterate over them and push the values into an array.

 var selected = [];
 $('#uniquedropdown').each( function() {
     selected.push( $(this).val() );
 });
tvanfosson
This will not work. `#` works for `id`, not names, and gives only the first match.
Kobi
+4  A: 

You can't use $('select[name="uniquedropdown"]').val() as that will return only the value of the first <select> in the page.

To get an an array of values

var values = $.map($('select[name="uniquedropdown"]'), function (e) {
                 return $('option:selected', e).val();
             });

or

var values = $.map($('select[name="uniquedropdown"]'), function (e) {
                 return $(e).val();
             });

Here's a Working Demo. add /edit to the URL to see the code

Russ Cam