使用jQuery在div中获取复选框列表

时间:2022-11-30 09:45:25

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中选择的。

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>

6 个解决方案

#1


353  

Combination of two previous answers:

结合之前的两个答案:

var selected = [];
$('#checkboxes input:checked').each(function() {
    selected.push($(this).attr('name'));
});

#2


40  

Would this do?

这个会做什么?

var selected = [];
$('div#checkboxes input[type=checkbox]').each(function() {
   if ($(this).is(":checked")) {
       selected.push($(this).attr('name'));
   }
});

#3


35  

$("#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;
});

#4


17  

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

将它与复选框的总数进行比较,看看它们是否相等。希望它能帮助某人

#5


7  

This works for me.

这适合我。

var selecteditems = [];

$("#Div").find("input:checked").each(function (i, ob) { 
    selecteditems.push($(ob).val());
});

#6


4  

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:

然后,您可以获得的价值,只有被勾选的值,使用地图:

$('#checkboxes input:checked[name="c_n[]"]')
            .map(function () { return $(this).val(); }).get()

#1


353  

Combination of two previous answers:

结合之前的两个答案:

var selected = [];
$('#checkboxes input:checked').each(function() {
    selected.push($(this).attr('name'));
});

#2


40  

Would this do?

这个会做什么?

var selected = [];
$('div#checkboxes input[type=checkbox]').each(function() {
   if ($(this).is(":checked")) {
       selected.push($(this).attr('name'));
   }
});

#3


35  

$("#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;
});

#4


17  

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

将它与复选框的总数进行比较,看看它们是否相等。希望它能帮助某人

#5


7  

This works for me.

这适合我。

var selecteditems = [];

$("#Div").find("input:checked").each(function (i, ob) { 
    selecteditems.push($(ob).val());
});

#6


4  

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:

然后,您可以获得的价值,只有被勾选的值,使用地图:

$('#checkboxes input:checked[name="c_n[]"]')
            .map(function () { return $(this).val(); }).get()