Jquery how to count checked and disable checkboxes

后端 未结 7 1463
有刺的猬
有刺的猬 2021-02-15 12:29

I am trying to disable all unchecked checkboxes when there are 5 checked checkboxes.

My code is not working here it is: http://jsfiddle.net/mtYtW/18/

My Jquery:

7条回答
  •  孤独总比滥情好
    2021-02-15 13:18

    I guess you want to disable the rest of the check boxes once the checked checkboxes count gets more than 5.If that is the case try this:

    $('table input[type="checkbox"]').click(function(){
        var countchecked = $('table input[type="checkbox"]').filter(":checked").length;
    
        if(countchecked > 4) {
            $("table input:checkbox").not(":checked").attr("disabled", true);
        } else {}
    });
    

    Working example: http://jsfiddle.net/mtYtW/30/

    If you want to disable the checkboxes on the page load and verify if there are more than 5 checkboxes that are checked then try this:

    $(function(){
        var countchecked = $('table input[type="checkbox"]').filter(":checked").length;
    
        if(countchecked > 4) {
            $("table input:checkbox").not(":checked").attr("disabled", true);
        } else {}
    });
    

提交回复
热议问题