使用 jquery 获取具有特定类名的所有选中的复选框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5450104/
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
Using jquery to get all checked checkboxes with a certain class name
提问by leora
I know I can get all checked checkboxes on a page using this:
我知道我可以使用这个在页面上获得所有选中的复选框:
$('input[type=checkbox]').each(function () {
var sThisVal = (this.checked ? $(this).val() : "");
});
But I am now using this on a page that has some other checkboxes that I don't want to include. How would I change the above code to only look at checked checkboxes that have a certain class on them?
但我现在在一个页面上使用它,该页面有一些我不想包含的其他复选框。我将如何更改上述代码以仅查看具有特定类的选中复选框?
回答by Cokegod
$('.theClass:checkbox:checked')
will give you all the checked checkboxes with the class theClass
.
$('.theClass:checkbox:checked')
将为您提供所有选中的复选框theClass
。
回答by Russ Cam
$('input:checkbox.class').each(function () {
var sThisVal = (this.checked ? $(this).val() : "");
});
An exampleto demonstrate.
一个例子来演示。
:checkbox
is a selector for checkboxes (in fact, you could omit the input
part of the selector, although I found niche cases where you would get strange results doing this in earlier versions of the library. I'm sure they are fixed in later versions).
.class
is the selector for element class attribute containing class
.
:checkbox
是复选框的选择器(实际上,您可以省略input
选择器的一部分,尽管我发现在某些特殊情况下,在早期版本的库中执行此操作会得到奇怪的结果。我确定它们在以后的版本中已修复)。
.class
是包含class
.
回答by karim79
回答by alienriver49
$('input.yourClass:checkbox:checked').each(function () {
var sThisVal = $(this).val();
});
This would get all checkboxes of the class name "yourClass". I like this example since it uses the jQuery selector checkedinstead of doing a conditional check. Personally I would also use an array to store the value, then use them as needed, like:
这将获得类名“yourClass”的所有复选框。我喜欢这个例子,因为它使用 jQuery 选择器检查而不是进行条件检查。我个人也会使用数组来存储值,然后根据需要使用它们,例如:
var arr = [];
$('input.yourClass:checkbox:checked').each(function () {
arr.push($(this).val());
});
回答by Faraz Kelhini
If you need to get the value of all checked checkboxesas an array:
如果您需要将所有选中复选框的值作为数组获取:
let myArray = (function() {
let a = [];
$(".checkboxes:checked").each(function() {
a.push(this.value);
});
return a;
})()
回答by Alp
$('input.theclass[type=checkbox]').each(function () {
var sThisVal = (this.checked ? $(this).val() : "");
});
回答by Florian Mertens
I'm not sure if it helps for your particular case, and I'm not sure if in your case, the checkboxes you want to include only are all part of a single form or div or table, but you can always select all checkboxes inside a specific element. For example:
我不确定它是否对您的特定情况有帮助,我不确定在您的情况下,您只想包含的复选框是否都是单个表单或 div 或表格的一部分,但您始终可以选择所有复选框在特定元素内。例如:
<ul id="selective">
<li><input type="checkbox" value="..." /></li>
<li><input type="checkbox" value="..." /></li>
<li><input type="checkbox" value="..." /></li>
<li><input type="checkbox" value="..." /></li>
</ul>
Then, using the following jQuery, it ONLY goes through the checkboxes within that UL with id="selective":
然后,使用以下 jQuery,它只通过 id="selective" 的 UL 中的复选框:
$('#selective input:checkbox').each(function () {
var sThisVal = (this.checked ? $(this).val() : "");
});
回答by Liza Daly
$('input.myclass[type=checkbox]').each(function () {
var sThisVal = (this.checked ? $(this).val() : ""); });
请参阅jQuery 类选择器。
回答by Caike Bispo
Simple way to get all of values into an array
将所有值放入数组的简单方法
var valores = (function () {
var valor = [];
$('input.className[type=checkbox]').each(function () {
if (this.checked)
valor.push($(this).val());
});
return valor;
})();
console.log(valores);
回答by Philip Voloshin
You can use something like this:
HTML:
你可以使用这样的东西:
HTML:
<div><input class="yourClass" type="checkbox" value="1" checked></div>
<div><input class="yourClass" type="checkbox" value="2"></div>
<div><input class="yourClass" type="checkbox" value="3" checked></div>
<div><input class="yourClass" type="checkbox" value="4"></div>
JQuery:
查询:
$(".yourClass:checkbox").filter(":checked")
It will choose values of 1 and 3.
它将选择值 1 和 3。