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# 控制相机设备 2022-01-01
- 获取C#保存对话框的文件路径 2022-01-01
- C# 9 新特性——record的相关总结 2023-04-03
- 将数据集转换为列表 2022-01-01
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- 如果条件可以为空 2022-01-01
