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中获取所有选定复选框值的最佳方法


基础教程推荐
- SSE 浮点算术是否可重现? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 如何激活MC67中的红灯 2022-01-01