2010-10-01 4 views
5

Quiero que se marque la casilla de verificación cuando estoy usando la función de alternar jquery. Funciona bien cuando uso el .bind('click').Casilla de verificación que no se comprueba al usar jquery toggle

$('#chkb').toggle(function() { 

    $('#va').text("checked"); 
    $('#chkb').attr('checked', 'checked'); 

}, 
function() { 

    $('#chkb').attr('checked', 'false'); 
    $('#va').text(""); 

}); 

html

<input type="checkbox" id="chkb" /> 

¿Cómo lo consigo.

Gracias Jean

+0

¿Qué tal esta chicos: var apchk = ''; . \t \t \t \t \t \t \t \t $ ('# c') html (apchk); – X10nD

+0

cómo * sobre * eso? –

Respuesta

1

lo que realmente tienen que checked en código? (¿Hay algo que había perdido? Casilla de verificación cuando se hace clic, ya basculante se comprueba el valor del atributo.)

mejor manera de hacer esto es mediante el uso de cambio como éste,

$('#chkb').change(function() { 
    var self = this; 
    $('#va').text(function(i,text){ 
     return self.checked?"checked":""; 
    }); 
}) 
+1

También puede marcar una casilla de verificación con el teclado. – ZeissS

+0

ahh ... así que es mejor usar '.change()' luego. ;) – Reigel

+0

¿Qué pasa con este var apchk = ''; . \t \t \t \t \t \t \t \t $ ('# c') html (apchk); – X10nD

2
chkb = $('#chkb'); 
va = $('#va'); 

chkb.change(function() { 
    va.text(chkb[0].checked ? "checked" : ""); 
}); 

selectores de antememoria a velocidades variables de las cosas - no tiene que buscar el dom cada vez. Poner chkb en una variable significa que se puede usar en cualquier lugar de su jQuery, en lugar de usar this.

+1

+1^_^un comentario sin embargo. Aunque jQuery te ayuda mucho, no lo abuse. 'chkb [0] .checked' es más rápido que' chkb.is (': checked') '. ¡aclamaciones! – Reigel

+0

@reigel, gracias ok se editará – NimChimpsky

+0

¿Quiere decir que [este código] (http://jsfiddle.net/VJj4R/) no funcionó en usted? – Reigel

Cuestiones relacionadas