简体   繁体   中英

.val() not working with jquery multiselect plugin

I looked at several other questions regarding this topic, but none of them helped. I am using this plugin and my select is defined like this:

<select name="test" multiple="multiple" id="test" style="width:150px">
    <option value="{{ cm }}">{{ cm }}</option>
    //several other fields
</select>

When the select field is changed, I want to print all selected fields with

var values = $('#test').val();
alert(values);

However, I only get an empty string... I am really sure that I don't have any syntax errors and virtually every source says that.val() can be used with a multiselect, what am I doing wrong?

Plugin you have mentioned doesn't use native select but list of input . As you can see in example author used this method to get values:

$("#submit_data").click(function( e ) {
   var fields = $( ":input" ).serializeArray(); // use your selector here, like #test input
   $( "#results" ).empty().append( JSON.stringify( fields , null, "\t") );
});

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