Obtener una lista de casillas de verificación marcadas en un div usando jQuery


Quiero obtener una lista de nombres de casillas de verificación que están seleccionadas en un div con cierto id. ¿Cómo lo haría usando jQuery?

Por ejemplo, para este div quiero obtener una matriz ["c_n_0"; "c_n_3" ] o una cadena "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

Combinación de dos respuestas anteriores:

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/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-04-16 12:41:27

¿Serviría esto?

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/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-11-08 19:00:54
$("#checkboxes").children("input:checked")

Te dará una matriz de los elementos mismos. Si solo necesita específicamente los nombres:

$("#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/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-01-28 15:46:34

Necesitaba el recuento de todas las casillas de verificación que están marcadas. En lugar de escribir un bucle hice esto

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

Compáralo con el número total de casillas de verificación para ver si son iguales. Espero que ayude a alguien

 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/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-04-25 19:18:19

Esto funciona para mí.

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/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-02-07 15:49:24

También podrías darles a todos el mismo nombre así que son una matriz, pero darles diferentes valores:

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

Entonces puede obtener solo el valor de solo los marcados usando map :

$('#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/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 12:03:06