使禁用复选框启用为假的问题

时间:2018-08-20 08:48:36

标签: javascript jquery

DerivedData
function checkenabledisable() {
  if ($("#completebatch").is(':checked')) {
    $('.commoncheckbox').prop("checked", true);
  } else {
    $('.commoncheckbox').prop("checked", false);
  }

  $('.disabled-check').removeAttr("checked");
}

表标题内的复选框用于全部选中和取消选中表主体中的所有复选框。上面的代码选中所有复选框。我希望它应该选中所有不具有“ disabled-check”类的复选框。上面的代码也选中了这些复选框...请帮助!!!

1 个答案:

答案 0 :(得分:2)

您的代码很好,除了最后一行应该是

$('.disabled-check').removeProp("checked"); // this is property not attribute

但是您也可以尝试以下解决方案。 您可以直接绑定更改事件处理程序以选中所有复选框,并使用is(":checked")将其值放入所有复选框,并放入.not(".disabled-check")以排除这些复选框

$(function(){
  $("#completebatch").on("change", function(){
    $('.commoncheckbox').not(".disabled-check").prop("checked", $(this).is(":checked"));
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<thead>
  <th><input id="completebatch" type="checkbox"></th>
</thead>

<tbody>
  <tr>
    <td>abc</td>
    <td><input class="commoncheckbox disabled-check" type="checkbox" disabled></td>
  </tr>
  <tr>
    <td>abc</td>
    <td><input class="commoncheckbox" type="checkbox"></td>
  </tr>
  <tr>
    <td>abc</td>
    <td><input class="commoncheckbox" type="checkbox"></td>
  </tr>
</tbody>

相关问题