jQuery Checkboxes

前端 未结 4 1697
长发绾君心
长发绾君心 2021-02-08 04:27

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

But, if one checkbox (any checkbox)

相关标签:
4条回答
  • 2021-02-08 04:49
    $(':checkbox').click(function () {
        $('li').toggleClass('disabled', !$(':checkbox:checked').length);
    });
    
    0 讨论(0)
  • 2021-02-08 04:51

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

    $(':checkbox').live('click', function () {
        $('li').toggleClass('disabled', !$(':checkbox:checked').length);
    });
    
    0 讨论(0)
  • 2021-02-08 04:52
    $(':checkbox')
        .click(
            function() 
            { 
                $('li').toggleClass('disabled', $(':checkbox :checked').length <= 0));
            }
         );
    

    EDIT: Thanks Ken for pointing out toggleClass method.

    0 讨论(0)
  • 2021-02-08 05:03

    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");
        }
    });
    
    0 讨论(0)
提交回复
热议问题