Cómo desmarcar la casilla de verificación usando jQuery Uniform library


Tengo un problema con desmarcar un checkbox. Echa un vistazo a mi jsFiddle , donde estoy intentando:

   $("#check2").attr("checked", true);

Uso uniforme para estilizar el checkboxy simplemente no funciona para marcar/desmarcar el checkbox.

¿Alguna idea?

Author: william.eyidi, 2011-02-14

12 answers

Mirando sus documentos, tienen una característica $.uniform.update para actualizar un elemento "uniformado".

Ejemplo: http://jsfiddle.net/r87NH/4/

$("input:checkbox").uniform();

$("body").on("click", "#check1", function () {
    var two = $("#check2").attr("checked", this.checked);
    $.uniform.update(two);
});
 108
Author: user113716,
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-03-17 00:45:38

Una solución más simple es hacer esto en lugar de usar uniforme:

$('#check1').prop('checked', true); // will check the checkbox with id check1
$('#check1').prop('checked', false); // will uncheck the checkbox with id check1

Esto no activará ninguna acción de clic definida.

También puedes usar:

$('#check1').click(); // 

Esto activará/desmarcará la casilla de verificación, pero también activará cualquier acción de clic que haya definido. Así que ten cuidado.

EDITAR : jQuery 1.6 + usa prop() no attr() para las casillas de verificación valor marcado

 368
Author: Mr.Hunt,
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-03-01 15:38:34
$("#chkBox").attr('checked', false); 

Esto funcionó para mí, esto desmarcará la casilla de verificación. De la misma manera podemos usar

$("#chkBox").attr('checked', true); 

Para marcar la casilla de verificación.

 24
Author: user1683014,
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-09-05 11:41:16

Si está utilizando uniform 1.5 entonces use este simple truco para agregar o eliminar el atributo de check
Simplemente agregue value="check" en el campo de entrada de su casilla de verificación.
Añadir este código en el uniform.js > function doCheckbox(elem){ > .click(function(){

if ( $(elem+':checked').val() == 'check' ) {
    $(elem).attr('checked','checked');           
}
else {
    $(elem).removeAttr('checked');
}   

Si no desea agregar value = "check" en su casilla de entrada porque en algunos casos agrega dos casillas de verificación, use esta

if ($(elem).is(':checked')) {
 $(elem).attr('checked','checked');
}    
else
{    
 $(elem).removeAttr('checked');
}

Si está utilizando uniform 2.0 entonces use este simple truco para agregar o eliminar el atributo de check
en este classUpdateChecked($tag, $el, options) { cambio de función

if ($el.prop) {
    // jQuery 1.6+
    $el.prop(c, isChecked);
}

A

if ($el.prop) {
    // jQuery 1.6+
    $el.prop(c, isChecked);
    if (isChecked) {
        $el.attr(c, c);
    } else {
        $el.removeAttr(c);
    }

}
 13
Author: Sohail Ahmed,
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-02-20 05:31:51
$('#check1').prop('checked', true).uniform(); 
$('#check1').prop('checked', false).uniform(); 

Esto funcionó para mí.

 7
Author: Parijat,
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-08-28 11:10:28

Solo haz esto:

$('#checkbox').prop('checked',true).uniform('refresh');
 2
Author: moledet,
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
2015-01-25 13:41:35

Debe llamar a $.uniform.update() si actualiza el elemento usando javascript como se menciona en la documentación.

 1
Author: Adeel,
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-02-14 20:44:10

En primer lugar, checked puede tener un valor de checked, o una cadena vacía.

$("input:checkbox").uniform();

$('#check1').live('click', function() {
    $('#check2').attr('checked', 'checked').uniform();
});
 1
Author: nyuszika7h,
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-02-14 20:49:16

En algún caso puedes usar esto:

$('.myInput').get(0).checked = true

Para alternar puede usar if else con la función

 1
Author: Larionov Nikita,
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-11-08 17:21:03

 $("#checkall").change(function () {
            var checked = $(this).is(':checked');
            if (checked) {
                $(".custom-checkbox").each(function () {
                    $(this).prop("checked", true).uniform();
                });
            } else {
                $(".custom-checkbox").each(function () {
                    $(this).prop("checked", false).uniform();
                });
            }
        });

        // Changing state of CheckAll custom-checkbox
        $(".custom-checkbox").click(function () {
            if ($(".custom-checkbox").length == $(".custom-checkbox:checked").length) {
                $("#chk-all").prop("checked", true).uniform();
            } else {
                $("#chk-all").removeAttr("checked").uniform();
            }
        });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" id='checkall' /> Select All<br/>
 <input type="checkbox" class='custom-checkbox' name="languages" value="PHP"> PHP<br/>
 <input type="checkbox" class='custom-checkbox' name="languages" value="AngularJS"> AngularJS<br/>
 <input type="checkbox" class='custom-checkbox' name="languages" value="Python"> Python<br/>
 <input type="checkbox" class='custom-checkbox' name="languages" value="Java"> Java<br/>
 1
Author: P.Mondal,
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-09-11 04:49:13

La otra forma de hacerlo es,

Use $('#check2').click();

Esto hace que uniform marque la casilla de verificación y actualice sus máscaras

 0
Author: Hailwood,
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-02-14 20:42:58

Casilla de verificación que actúan como radio btn

$(".checkgroup").live('change',function() { var previous=this.checked; $(".checkgroup).attr("checked", false); $(this).attr("checked", previous); });

 -1
Author: RaDo,
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-06-02 21:58:23