简体   繁体   English

选中表单中的任何/没有复选框时,启用/禁用提交按钮

[英]Enable/disable submit button when any/no checkbox in form is checked

I am trying to use jQuery to dynamically enable/disable a submit button for a form. 我正在尝试使用jQuery动态地启用/禁用表单的提交按钮。

The HTML: HTML:

<form id="user-add-to-group">
  <table class="group-list">
    <tbody>
      <tr>
        <th>Voornaam</th>
        <th>Achternaam</th>
        <th>S/P-nummer</th>
        <th>Email</th>
        <th>Type</th>
      </tr>
      <tr>   
        <td>Name</td>
        <td>Surname</td>
        <td>No.</td>
        <td>email@bs.com</td>
        <td>student</td>
        <td><input type="checkbox" name="user_add_to_group[]" value="75F0CE8F-4B6F-4E17-BC7B-0B06AAD84625"></td>
      </tr>
    </tbody>
  </table>
  <button disabled="disabled" type="button" id="participant_add_btn" name="participant_add_btn" class="btn btn-primary">Add</button>                            
</form>

And the jQuery: 和jQuery:

$('table.group-list tr').on("click", function(){
  $(this).find("input[type=checkbox]").trigger('click');
    if($("#user-add-to-group input[type=checkbox]:checked").length>0){
      $("#participant_add_btn").prop("disabled","false");
    }
    else {
      $("#participant_add_btn").prop("disabled","disabled");
    }
});

I want to make the entire table row clickable, so the jQuery triggers a click event when the tr is clicked. 我想使整个表行都可单击,因此当单击tr时,jQuery会触发click事件。 This works, but I cannot seem to figure out why my button won't be enabled afterwards. 这行得通,但是我似乎无法弄清楚为什么以后无法启用我的按钮。 I want to enable it when any checkbox within the form is checked, and disable it when none of them are. 我想在选中表单中的任何复选框时启用它,而当它们都没有时禁用它。

If more info is needed, please let me know. 如果需要更多信息,请告诉我。

You need to pass Boolean argument instead of string while setting the property value. 在设置属性值时,您需要传递布尔参数而不是字符串。

$('#participant_add_btn').prop('disabled', false);

or use .removeAttr("disabled") 或使用.removeAttr("disabled")

$('#participant_add_btn').removeAttr("disabled");

Your code will not work on click of checkbox. 单击复选框后,您的代码将无法使用。 For that,you need to check the target of clicked element, if it is already checkbox then do not trigger the click event for it: 为此,您需要检查clicked元素的目标,如果它已经是复选框,则不要为其触发click事件:

Working Snippet: 工作片段:

 $('table.group-list tr').on("click", function(e){ if($(e.target).is(':not(:checkbox)')) $(this).find("input[type=checkbox]").trigger('click'); $("#participant_add_btn").prop("disabled",$("#user-add-to-group input[type=checkbox]:checked").length == 0); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form id="user-add-to-group"> <table class="group-list"> <tbody> <tr> <th>Voornaam</th> <th>Achternaam</th> <th>S/P-nummer</th> <th>Email</th> <th>Type</th> </tr> <tr> <td>Name</td> <td>Surname</td> <td>No.</td> <td>email@bs.com</td> <td>student</td> <td><input type="checkbox" name="user_add_to_group[]" value="75F0CE8F-4B6F-4E17-BC7B-0B06AAD84625"></td> </tr> </tbody> </table> <button disabled="disabled" type="button" id="participant_add_btn" name="participant_add_btn" class="btn btn-primary">Add</button> </form> 

jQuery.prop expects value to be Boolean , any string which is not empty is considred as true as it is truthy value jQuery.prop期望value为Boolean ,任何不为空的string都视为true因为它是true值

 $('table.group-list tr').on("click", function() { $(this).find("input[type=checkbox]").trigger('click'); $("#participant_add_btn").prop("disabled", !$("#user-add-to-group input[type=checkbox]:checked").length); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <form id="user-add-to-group"> <table class="group-list"> <tbody> <tr> <th>Voornaam</th> <th>Achternaam</th> <th>S/P-nummer</th> <th>Email</th> <th>Type</th> </tr> <tr> <td>Name</td> <td>Surname</td> <td>No.</td> <td>email@bs.com</td> <td>student</td> <td> <input type="checkbox" name="user_add_to_group[]" value="75F0CE8F-4B6F-4E17-BC7B-0B06AAD84625"> </td> </tr> </tbody> </table> <button disabled="disabled" type="button" id="participant_add_btn" name="participant_add_btn" class="btn btn-primary">Add</button> </form> 

$('#toggle').click(function () {
//check if checkbox is checked
if ($(this).is(':checked')) {

    $('#sendNewSms').removeAttr('disabled'); //enable input

} else {
    $('#sendNewSms').attr('disabled', true); //disable input
}

}); });

Check here. 在这里检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM