How to enable submit button if at least two checkboxes are checked?

后端 未结 3 890
日久生厌
日久生厌 2021-01-28 22:42

With the help of answers I found here, I try to disable submit button and send an alert message when clicked on it until there\'s not at least 2 checkboxes checked.

What

相关标签:
3条回答
  • 2021-01-28 23:03

    You initialize the count of checked checkboxes just once, when your script is first parsed. The count will not be recomputed later. This line:

    var selected = $('#frmCompare :checkbox:checked').length;
    

    should be inside the verification function, not outside.

    0 讨论(0)
  • 2021-01-28 23:07

    There is no enabled attribute in HTML.

    $('#btnCompare').prop('disabled', selected < 2);
    

    You also need to recalculate the value of selected at every change, you can't just go with what it was set to at page load.

    0 讨论(0)
  • 2021-01-28 23:13

    You should change your code as

    $('#frmCompare :checkbox').change(function(){
    
      //update selected variable
      selected = $('#frmCompare :checkbox:checked').length
    
      if (selected >= 2) {
         $('#btnCompare').attr('enabled');
      }
    });
    
    0 讨论(0)
提交回复
热议问题