使用 jQuery 获取 div 中选中的复选框列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2155622/
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
Get a list of checked checkboxes in a div using jQuery
提问by Juozas Kontvainis
I want to get a list of names of checkboxes that are selected in a div with certain id. How would I do that using jQuery?
我想获取在具有特定 id 的 div 中选择的复选框的名称列表。我将如何使用 jQuery 做到这一点?
E.g., for this div I want to get array ["c_n_0"; "c_n_3"] or a string "c_n_0;c_n_3"
例如,对于这个 div,我想得到数组 ["c_n_0"; "c_n_3"] 或字符串 "c_n_0;c_n_3"
<div id="checkboxes">
<input id="chkbx_0" type="checkbox" name="c_n_0" checked="checked" />Option 1
<input id="chkbx_1" type="checkbox" name="c_n_1" />Option 2
<input id="chkbx_2" type="checkbox" name="c_n_2" />Option 3
<input id="chkbx_3" type="checkbox" name="c_n_3" checked="checked" />Option 4
</div>
回答by Alex LE
Combination of two previous answers:
结合之前的两个答案:
var selected = [];
$('#checkboxes input:checked').each(function() {
selected.push($(this).attr('name'));
});
回答by nikc.org
Would this do?
这行吗?
var selected = [];
$('div#checkboxes input[type=checkbox]').each(function() {
if ($(this).is(":checked")) {
selected.push($(this).attr('name'));
}
});
回答by Corey
$("#checkboxes").children("input:checked")
will give you an array of the elements themselves. If you just specifically need the names:
会给你一个元素本身的数组。如果您只是特别需要名称:
$("#checkboxes").children("input:checked").map(function() {
return this.name;
});
回答by Usman Shaukat
I needed the count of all checkboxes which are checked. Instead of writing a loop i did this
我需要所有选中的复选框的计数。我没有写一个循环,而是这样做了
$(".myCheckBoxClass:checked").length;
Compare it with the total number of checkboxes to see if they are equal. Hope it will help someone
将它与复选框的总数进行比较,看看它们是否相等。希望它会帮助某人
回答by Ricardo
This works for me.
这对我有用。
var selecteditems = [];
$("#Div").find("input:checked").each(function (i, ob) {
selecteditems.push($(ob).val());
});
回答by SharpC
You could also give them all the same name so they are an array, but give them different values:
你也可以给它们一个相同的名字,所以它们是一个数组,但给它们不同的值:
<div id="checkboxes">
<input type="checkbox" name="c_n[]" value="c_n_0" checked="checked" />Option 1
<input type="checkbox" name="c_n[]" value="c_n_1" />Option 2
<input type="checkbox" name="c_n[]" value="c_n_2" />Option 3
<input type="checkbox" name="c_n[]" value="c_n_3" checked="checked" />Option 4
</div>
You can then get only the value of only the ticked ones using map:
然后,您可以使用 map仅获取勾选值的值:
$('#checkboxes input:checked[name="c_n[]"]')
.map(function () { return $(this).val(); }).get()
回答by jamaljaj
function listselect() {
var selected = [];
$('.SelectPhone').prop('checked', function () {
selected.push($(this).val());
});
alert(selected.length);
<input type="checkbox" name="SelectPhone" class="SelectPhone" value="1" />
<input type="checkbox" name="SelectPhone" class="SelectPhone" value="2" />
<input type="checkbox" name="SelectPhone" class="SelectPhone" value="3" />
<button onclick="listselect()">show count</button>
回答by David Blanco
var agencias = [];
$('#Div input:checked').each(function(index, item){
agencias.push(item.nextElementSibling.attributes.for.nodeValue);
});