在 jQuery 中获取所有选定复选框值的最佳方法 [英] Best way to get all selected checkboxes VALUES in jQuery

查看:26
本文介绍了在 jQuery 中获取所有选定复选框值的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望获得通过 jQuery 选择的所有复选框的值.

I am looking to get all checkboxes' VALUE which have been selected through jQuery.

推荐答案

您希望 :checkbox:checked 选择器和 map 创建一个值数组:

You want the :checkbox:checked selector and map to create an array of the values:

var checkedValues = $('input:checkbox:checked').map(function() {
    return this.value;
}).get();

如果您的复选框有一个共享类,那么使用它会更快,例如.$('.mycheckboxes:checked'),或用于通用名称 $('input[name="Foo"]:checked')

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')

- 更新 -

如果您不需要 IE 支持,那么您现在可以使用箭头函数使 map() 调用更加简洁:

If you don't need IE support then you can now make the map() call more succinct by using an arrow function:

var checkedValues = $('input:checkbox:checked').map((i, el) => el.value).get();

这篇关于在 jQuery 中获取所有选定复选框值的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆