我有一组同名的选择框。我想通过 jQuery 获取那些选择框的选定值并创建一个数组。
function submitboxes(){
var manufCountry = $('input[name=manufacturerCountry]').map(function(){
return this.value;
}).get();
console.log(manufCountry);
}
<form><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="manufacturerCountry[]" id="" class="form-control customSelect"><option value="1">A</option><option value="2">B</option></select>
<select name="manufacturerCountry[]" id="" class="form-control customSelect"><option value="3">C</option><option value="4">D</option></select>
<button type="button" onclick="submitboxes();">Submit</button>
</form>
但是,在控制台中,我看到
[]
您的数组始终为空,因为您的选择器与您的 HTML 不匹配:
$('input[name=manufacturerCountry]')
将搜索input
name 属性等于 的manufacturerCountry
。
但是,您的 HTML 包含一个select
名为的元素manufacturerCountry[]
。因此,当您将选择器更改为
$('select[name^=manufacturerCountry')
您的代码将正常工作。
注意:我已更改事件处理以删除该onclick
属性
参考:
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句