jQuery:无法获取同名选择框的值

Niladri Banerjee - Uttarpara

我有一组同名的选择框。我想通过 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]')将搜索inputname 属性等于 的manufacturerCountry

但是,您的 HTML 包含一个select名为元素manufacturerCountry[]因此,当您将选择器更改为

$('select[name^=manufacturerCountry')

您的代码将正常工作。


例子

注意:我已更改事件处理以删除该onclick属性


参考:

jQuery 属性选择器

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章