tag name modify data change bootstrap body attribute jquery unchecked

jquery - name - Catch verificado evento de cambio de una casilla de verificación



jquery modify title tag (9)

¿Cómo puedo activar / desactivar evento de <input type="checkbox" /> con jQuery?


El clic afectará a una etiqueta si tenemos una adjunta a la casilla de verificación de entrada?

Creo que es mejor usar la función .change ()

<input type="checkbox" id="something" /> $("#something").change( function(){ alert("state changed"); });


En mi experiencia, tuve que aprovechar el objetivo actual del evento:

$("#dingus").click( function (event) { if ($(event.currentTarget).is('':checked'')) { //checkbox is checked } });


Este código hace lo que necesita:

<input type="checkbox" id="check" >check it</input> $("#check").change( function(){ if( $(this).is('':checked'') ) { alert("checked"); }else{ alert("unchecked"); } });

Además, puedes verificarlo en jsfiddle


Para el uso de JQuery 1.7+:

$(''input[type=checkbox]'').on(''change'', function() { ... });


Use el selector :checked para determinar el estado de la casilla de verificación:

$(''input[type=checkbox]'').click(function() { if($(this).is('':checked'')) { ... } else { ... } });


Utilice el siguiente fragmento de código para lograr esto .:

$(''#checkAll'').click(function(){ $("#checkboxes input").attr(''checked'',''checked''); }); $(''#UncheckAll'').click(function(){ $("#checkboxes input").attr(''checked'',false); });

O puede hacer lo mismo con una sola casilla de verificación:

$(''#checkAll'').click(function(e) { if($(''#checkAll'').attr(''checked'') == ''checked'') { $("#checkboxes input").attr(''checked'',''checked''); $(''#checkAll'').val(''off''); } else { $("#checkboxes input").attr(''checked'', false); $(''#checkAll'').val(''on''); } });

Para demostración: http://jsfiddle.net/creativegala/hTtxe/


utilice el evento click para la mejor compatibilidad con MSIE

$(document).ready(function() { $("input[type=checkbox]").click(function() { alert("state changed"); }); });


$(document).ready(function(){ checkUncheckAll("#select_all","[name=''check_boxes[]'']"); }); var NUM_BOXES = 10; // last checkbox the user clicked var last = -1; function check(event) { // in IE, the event object is a property of the window object // in Mozilla, event object is passed to event handlers as a parameter event = event || window.event; var num = parseInt(/box/[(/d+)/]/.exec(this.name)[1]); if (event.shiftKey && last != -1) { var di = num > last ? 1 : -1; for (var i = last; i != num; i += di) document.forms.boxes[''box['' + i + '']''].checked = true; } last = num; } function init() { for (var i = 0; i < NUM_BOXES; i++) document.forms.boxes[''box['' + i + '']''].onclick = check; }

HTML:

<body onload="init()"> <form name="boxes"> <input name="box[0]" type="checkbox"> <input name="box[1]" type="checkbox"> <input name="box[2]" type="checkbox"> <input name="box[3]" type="checkbox"> <input name="box[4]" type="checkbox"> <input name="box[5]" type="checkbox"> <input name="box[6]" type="checkbox"> <input name="box[7]" type="checkbox"> <input name="box[8]" type="checkbox"> <input name="box[9]" type="checkbox"> </form> </body>


<input type="checkbox" id="something" /> $("#something").click( function(){ if( $(this).is('':checked'') ) alert("checked"); });

Editar: Hacer esto no se detectará cuando la casilla de verificación cambie por otras razones que no sean un clic, como usar el teclado. Para evitar este problema, escuche el change lugar de click .

Para marcar / desmarcar mediante programación, eche un vistazo a ¿Por qué no se activó mi casilla de verificación?