开发者

getting values of multiple selectboxes via jquery

I want to fill an array in javascript that takes all the values out of different selectboxes with the same name

so for example if i have a html like this:

<select name="selectbox[]">
<option value="value1">text1</option>
<option value="value2">text2</option>
</select>

<select name="selectbox[]">
<option value开发者_如何学C="value1">text1</option>
<option value="value2">text2</option>
</select>

I have to get the selected values of both selectboxes and put it in an array.

what i did:

 $("input[name=selectbox\\[\\]]").map(function(){return $(this).val();});

does not work.

A similar example wich does work for textboxes:

$("input[name=textbox\\[\\]]").map(function(){return this.value;});

Thanks for your help!


Perhaps because its a select and not an input ??

var $select = $("<select multiple='multiple' />");
for (var x=0; x<3; x++) {
  $select.append("<option value='"+x+"' selected='selected'>"+x+"</option>");
}
console.log($select.val());
//  ["0", "1", "2"]
console.log($select.map(function() { return $(this).val() }));
//  jQuery("0", "1", "2")

// create a second select:
$select = $select.clone(true).andSelf();
console.log($select.map(function() { return $(this).val() }));
//  jQuery("0", "1", "2", "0", "1", "2")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜