如何使用 jQuery Uniform 库取消选中复选框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4996953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:21:11  来源:igfitidea点击:

How to uncheck checkbox using jQuery Uniform library

jquerycheckbox

提问by Upvote

I have a problem with unchecking a checkbox. Have a look at my jsFiddle, where I am attempting:

我在取消选中checkbox. 看看我的 jsFiddle,我正在尝试:

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

I use uniformfor styling the checkboxand it simply does not work to check/uncheckthe checkbox.

我使用统一的造型的checkbox,它根本不工作,检查/取消选中checkbox

Any ideas?

有任何想法吗?

采纳答案by user113716

Looking at their docs, they have a $.uniform.updatefeature to refresh a "uniformed" element.

查看他们的文档,他们有一个$.uniform.update功能可以刷新“统一”元素。

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

示例:http : //jsfiddle.net/r87NH/4/

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

$("body").on("click", "#check1", function () {
    var two = $("#check2").attr("checked", this.checked);
    $.uniform.update(two);
});

回答by Mr.Hunt

A simpler solution is to do this rather than using uniform:

一个更简单的解决方案是这样做而不是使用统一:

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

This will not trigger any click action defined.

这不会触发任何定义的点击操作。

You can also use:

您还可以使用:

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

This will toggle the check/uncheck for the checkbox but this will also trigger any click action you have defined. So be careful.

这将切换复选框的选中/取消选中,但这也会触发您定义的任何点击操作。所以要小心。

EDIT: jQuery 1.6+ uses prop()not attr()for checkboxes checked value

编辑:jQuery 1.6+prop()attr()用于复选框检查值

回答by user1683014

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

This worked for me, this will uncheck the check box. In the same way we can use

这对我有用,这将取消选中复选框。以同样的方式我们可以使用

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

to check the checkbox.

选中复选框。

回答by Sohail Ahmed

If you are using uniform 1.5then use this simple trick to add or remove attribute of check
Just add value="check" in your checkbox's input field.
Add this code in uniform.js> function doCheckbox(elem){> .click(function(){

如果您使用的是uniform 1.5,那么使用这个简单的技巧来添加或删除检查属性
只需在复选框的输入字段中添加 value="check" 即可。
在添加该代码uniform.js> function doCheckbox(elem){>.click(function(){

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

if you not want to add value="check" in your input box because in some cases you add two checkboxes so use this

如果你不想在你的输入框中添加 value="check" 因为在某些情况下你添加了两个复选框所以使用这个

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

If you are using uniform 2.0then use this simple trick to add or remove attribute of check
in this classUpdateChecked($tag, $el, options) {function change

如果您使用的是uniform 2.0,那么使用这个简单的技巧来添加或删除
classUpdateChecked($tag, $el, options) {函数更改中的检查属性

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

To

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

}

回答by Parijat

$('#check1').prop('checked', true).uniform(); 
$('#check1').prop('checked', false).uniform(); 

This worked for me.

这对我有用。

回答by moledet

Just do this:

只需这样做:

$('#checkbox').prop('checked',true).uniform('refresh');

回答by P.Mondal

 $("#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/>

回答by Larionov Nikita

In some case you can use this:

在某些情况下,您可以使用它:

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

For toggle you can use if else with function

对于切换,您可以使用 if else with function

回答by Adeel

you need to call $.uniform.update()if you update element using javascript as mentioned in the documentation.

$.uniform.update()如果您使用文档中提到的 javascript 更新元素,则需要调用。

回答by nyuszika7h

First of all, checkedcan have a value of checked, or an empty string.

首先,checked可以有一个值checked,或者一个空字符串。

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

$('#check1').live('click', function() {
    $('#check2').attr('checked', 'checked').uniform();
});