buscar todos sin marcar casilla de verificación en jquery
Tengo una lista de casillas de verificación:
<input type="checkbox" name="answer" id="id_1' value="1" />
<input type="checkbox" name="answer" id="id_2' value="2" />
...
<input type="checkbox" name="answer" id="id_n' value="n" />
Puedo recopilar todos los valores de las casillas de verificación marcadas; mi pregunta es ¿cómo puedo obtener todos los valores de las casillas de verificación sin marcar? Lo intenté:
$("input:unchecked").val();
Para obtener el valor de una casilla de verificación sin marcar, pero obtuve:
Error de sintaxis, expresión no reconocida: sin marcar.
¿Puede alguien arrojar luz sobre este tema? ¡Gracias!
7 answers
Como indica el mensaje de error, jQuery no incluye un selector :unchecked
.
En su lugar, necesita invertir el selector :checked
:
$("input:checkbox:not(:checked)")
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
2011-12-11 17:09:59
$("input:checkbox:not(:checked)")
Te dará las casillas sin marcar.
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
2011-12-11 17:09:28
$.extend($.expr[':'], {
unchecked: function (obj) {
return ((obj.type == 'checkbox' || obj.type == 'radio') && !$(obj).is(':checked'));
}
});
$("input:unchecked")
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
2012-05-19 09:23:49
$("input[type='checkbox']:not(:checked):not('\#chkAll\')").map(function () {
var a = "";
if (this.name != "chkAll") {
a = this.name + "|off";
}
return a;
}).get().join();
Esto recuperará todas las casillas de verificación sin marcar y excluirá la casilla de verificación "chkAll" que uso para marcar|desmarcar todas las casillas de verificación. Ya que quiero saber qué valor estoy pasando a la base de datos los establezco en off, ya que las casillas de verificación me dan un valor de on.
//looking for unchecked checkboxes, but don’t include the checkbox all that checks or unchecks all checkboxes
//.map - Pass each element in the current matched set through a function, producing a new jQuery object containing the return values.
//.get - Retrieve the DOM elements matched by the jQuery object.
//.join - (javascript) joins the elements of an array into a string, and returns the string.The elements will be separated by a specified separator. The default separator is comma (,).
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
2013-05-24 16:34:30
Puedes usar así:
$(":checkbox:not(:checked)")
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
2016-07-31 07:28:11
$(".clscss-row").each(function () {
if ($(this).find(".po-checkbox").not(":checked")) {
// enter your code here
} });
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-12-20 07:17:56
También se puede lograr con js puro de tal manera:
var matches = document.querySelectorAll('input[type="checkbox"]:not(:checked)');
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
2018-08-14 14:38:06