jQuery 复选框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1160238/
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 10:48:03  来源:igfitidea点击:

jQuery Checkboxes

jquerycheckbox

提问by RaYell

I'm trying to write a piece of jQuery code where, if all checkboxes are "unchecked", then all li tags have the class "disabled."

我正在尝试编写一段 jQuery 代码,其中,如果所有复选框都“未选中”,则所有 li 标签都将“禁用”类。

But, if one checkbox (any checkbox) is checked, then all [li] tags lose the class "disabled".

但是,如果选中了一个复选框(任何复选框),那么所有 [li] 标签都会失去“禁用”类。

Many thanks!

非常感谢!

回答by RaYell

$(':checkbox').click(function () {
    $('li').toggleClass('disabled', !$(':checkbox:checked').length);
});

回答by Garytxo

I came across this post by accident and I thought I would add my shilling worth:

我偶然看到了这篇文章,我想我会增加我的先令价值:

jQuery(':checkbox').click(function()
{
    if (jQuery(this).is(':checked'))
    {
        alert("Checked");
    }
    else
    {
        alert("Unchecked");
    }
});

回答by Matt Sach

Slight modification of RaYell's, which will include any dynamically added checkboxes:

对 RaYell 的轻微修改,包括任何动态添加的复选框:

$(':checkbox').live('click', function () {
    $('li').toggleClass('disabled', !$(':checkbox:checked').length);
});

回答by SolutionYogi

$(':checkbox')
    .click(
        function() 
        { 
            $('li').toggleClass('disabled', $(':checkbox :checked').length <= 0));
        }
     );

EDIT:Thanks Ken for pointing out toggleClass method.

编辑:感谢 Ken 指出 toggleClass 方法。