在 jQuery 中获取所有选定复选框值的最佳方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19766044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best way to get all selected checkboxes VALUES in jQuery
提问by hercules
I am looking to get all checkboxes' VALUE which have been selected through jQuery.
我希望获得通过 jQuery 选择的所有复选框的值。
回答by Rory McCrossan
You want the :checkbox:checked
selector and map
to create an array of the values:
您需要:checkbox:checked
选择器并map
创建一个值数组:
var checkedValues = $('input:checkbox:checked').map(function() {
return this.value;
}).get();
If your checkboxes have a shared class it would be faster to use that instead, eg. $('.mycheckboxes:checked')
, or for a common name $('input[name="Foo"]:checked')
如果您的复选框有一个共享类,那么使用它会更快,例如。$('.mycheckboxes:checked')
, 或通用名称$('input[name="Foo"]:checked')
- Update -
- 更新 -
If you don't need IE support then you can now make the map()
call more succinct by using an arrow function:
如果您不需要 IE 支持,那么您现在可以map()
使用箭头函数使调用更加简洁:
var checkedValues = $('input:checkbox:checked').map((i, el) => el.value).get();