Pobierz listę zaznaczonych checkboxów w div za pomocą jQuery

Chcę uzyskać listę nazw checkboxów, które są zaznaczone w div o określonym id. Jak mam to zrobić używając jQuery?

Np. dla tego div chcę uzyskać tablicę ["c_n_0"; "c_n_3"] lub łańcuch "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>
Author: Azim, 2010-01-28

6 answers

Połączenie dwóch poprzednich odpowiedzi:

var selected = [];
$('#checkboxes input:checked').each(function() {
    selected.push($(this).attr('name'));
});
 369
Author: Alex LE,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-04-16 12:41:27

Czy to wystarczy?

var selected = [];
$('div#checkboxes input[type=checkbox]').each(function() {
   if ($(this).is(":checked")) {
       selected.push($(this).attr('name'));
   }
});
 43
Author: nikc.org,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-11-08 19:00:54
$("#checkboxes").children("input:checked")

Da ci tablicę samych elementów. Jeśli potrzebujesz tylko nazw:

$("#checkboxes").children("input:checked").map(function() {
    return this.name;
});
 35
Author: Corey,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-01-28 15:46:34

Potrzebowałem policzenia wszystkich zaznaczonych pól. Zamiast pisać pętlę zrobiłem to

$(".myCheckBoxClass:checked").length;

Porównaj go z całkowitą liczbą pól wyboru, aby sprawdzić, czy są równe. Mam nadzieję, że to komuś pomoże

 18
Author: Usman Shaukat,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-04-25 19:18:19

To mi pasuje.

var selecteditems = [];

$("#Div").find("input:checked").each(function (i, ob) { 
    selecteditems.push($(ob).val());
});
 7
Author: Ricardo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-07 15:49:24

Możesz również nadać im wszystkie te same nazwy więc są tablicą , ale daj im inne wartości:

<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>

Możesz wtedy uzyskać tylko wartość tylko zaznaczonych za pomocą mapy :

$('#checkboxes input:checked[name="c_n[]"]')
            .map(function () { return $(this).val(); }).get()
 4
Author: SharpC,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:03:06