Best way to get all selected checkboxes VALUES in jQuery(在jQuery中获取所有选定复选框值的最佳方法)
问题描述
我正在寻找通过 jQuery 选择的所有复选框的 VALUE.
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中获取所有选定复选框值的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在jQuery中获取所有选定复选框值的最佳方法


- C#MongoDB使用Builders查找派生对象 2022-09-04
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 输入按键事件处理程序 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01