Jquery how to count checked and disable checkboxes

后端 未结 7 1436
有刺的猬
有刺的猬 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:04

    The following should do the trick for your needs:

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

    });

    Example for your needs

    (Generic) The following will disable all of your unchecked checkboxes:

    $('input[type=checkbox]').not(':checked').attr("disabled","disabled");
    

    Generic Disable Example

提交回复
热议问题