Disable button if all checkboxes are unchecked and enable it if at least one is checked

后端 未结 4 1177
轮回少年
轮回少年 2021-02-15 16:12

I have a table with a checkbox in each row and a button below it. I want to disable the button if at least one checkbox is checked.


    
         


        
4条回答
  •  北荒
    北荒 (楼主)
    2021-02-15 16:53

    Add an event handler that fires when a checkbox is changed, and see if there are any checked boxes, and set the disabled property appropriately :

    var boxes = $('.myCheckBox');
    
    boxes.on('change', function() {
        $('#confirmButton').prop('disabled', !boxes.filter(':checked').length);
    }).trigger('change');
    

    FIDDLE

提交回复
热议问题