简体   繁体   中英

How can I get a multiple selected values in dropdown

I am using drop down with multiple select name defined with select[]

How can I get selected values using jquery.

The same way as any form element - use val() .

var selectedValues = $("#select").val();

With a multiple select you will see the value as a comma delimited string which can easily be posted for server-side processing or split into an array if required.

Example fiddle

If someone wants values with labels . Then here is the solution:

var hexvalues = [];
var labelvalues = [];

$('#myMultiSelect :selected').each(function(i, selectedElement) {
 hexvalues[i] = $(selectedElement).val();
 labelvalues[i] = $(selectedElement).text();
});

Try this,

Live Demo

$('#btn').click(function(){
    $('#select option:selected').each(function(){
        alert($(this).text());
    });
})​

you should try this:

$("select[name^='select[']:eq(0)").val();

remember, that eq(0) is indicated what is the index of your element with the same name.

Try

 var selectedItems= $('#ddlId option:selected');

selectedItems.each(function(obj,ind){
     $(obj).val() ;
} // or do with for (var i=0// normal js loop

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM