如何选择全部/取消选中未禁用的所有复选框?

时间:2016-07-05 06:17:32

标签: javascript jquery html checkbox dynamic-content

我有多个复选框输入,而有些则被禁用。因此,当我单击选择全部/取消选中顶部的所有复选框时,我想将此事件应用于仅启用的复选框。此外,当我通过单击每个复选框检查所有复选框时,必须单击全选复选框。

注意:此外我还有一项功能,当点击启用的复选框并点击保存点击后,该特定复选框将被禁用。我可以使用添加新复选框添加新复选框按钮。添加新复选框后,必须取消选中全选/取消全选复选框。

HTML

combn(1:3, 2, FUN = function(i) list(do.call(cbind, x[i])))
#[[1]]
#     [,1] [,2]
#[1,]    1    4
#[2,]    2    5
#[3,]    3    6

#[[2]]
#     [,1] [,2]
#[1,]    1    7
#[2,]    2    8
#[3,]    3    9

#[[3]]
#     [,1] [,2]
#[1,]    4    7
#[2,]    5    8
#[3,]    6    9

JQuery的

<input type="checkbox" id="ckbCheckAll" /><span>Select all</span>
<table id="myTable">
  <tr>
    <td>
      <input type="checkbox" class="checkBoxClass" disabled="disabled" checked/> <span>Option1</span></td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" class="checkBoxClass" /> <span>Option2</span></td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" class="checkBoxClass" /> <span>Option3</span></td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" class="checkBoxClass" /> <span>Option4</span></td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" class="checkBoxClass" /> <span>Option5</span></td>
  </tr>
</table>
<button id="add">
  Add check box
</button>
<button id="save">
  Save
</button>

Fiddle

4 个答案:

答案 0 :(得分:2)

要检测已禁用的复选框,您可以使用jquery filter方法,如下所示:

$(".checkBoxClass").filter(function(){
    return !$(this).attr('disabled');
}).prop('checked', $(this).prop('checked'));

通过使用filter这样的方式,JQuery更改了未禁用属性的复选框。

并使用.prop('checked', false).attr('checked', false)取消选中添加新复选框按钮的复选框。

所以要取消选中全部保存并添加复选框使用:

$("#ckbCheckAll").attr('checked', false);

并取消选中addcheckbox上的所有已启用复选框:

$(".checkBoxClass").filter(function(){
    return !$(this).attr('disabled');
}).prop('checked', false);

您还应该使用$(document).on("click",".checkBoxClass",function(){})绑定器让JQuery绑定click事件以动态添加复选框。通过执行此操作并添加一些额外的jquery,选中所有复选框,选中所有复选框时选中。

Updated Fiddle

答案 1 :(得分:0)

试试这个,它只会启用checkobox并选择/取消选择它们。

$(document).ready(function() {
  $("#ckbCheckAll").click(function() {
    $(".checkBoxClass:enabled").prop('checked', $(this).checked);
  });
});

检查小提琴 http://jsfiddle.net/p73wW/8/

答案 2 :(得分:0)

试试这个:

$(document).ready(function() {
$("#ckbCheckAll").click(function() {
      var checked = false;
      if($(this).prop('checked') == true) {
          checked = true;
      }
      $("#myTable tbody tr").each(function() {
          if($(this).find('.checkBoxClass').attr('disabled') != 'disabled') {
              $(this).find(".checkBoxClass").prop('checked', checked);
          }
      });
});

$('.checkBoxClass').click(function() {
    var i = 1;
    var totalCheckboxes = $("#myTable tbody tr").length;
    if($(this).prop('checked') == true) {
        $("#myTable tbody tr").each(function() {
            if($(this).find('.checkBoxClass').attr('disabled') != 'disabled') {
                if($(this).find(".checkBoxClass").prop('checked') == true) {
                    i++;
                }
            }
        });
    }
    if(i==totalCheckboxes) {
        $("#ckbCheckAll").prop('checked', true);
    } else {
        $("#ckbCheckAll").prop('checked', false);
    }
});

count = 5;
$('#save').click(function() {
    $('.checkBoxClass').each(function() {
        if ($(this).is(':checked')) {
            $(this).prop('disabled', true);
        }
    });
    $("#ckbCheckAll").prop('checked', false);
});

$('#add').click(function() {
    count = count + 1;
    $('#myTable').append('<tr><td><input type="checkbox" class="checkBoxClass" /> <span>Option'+count+'</span></td></tr>')
});
});

Updated Fiddle

答案 3 :(得分:0)

这是另一种方法。我使用filter而不是:enabled。还使用on jquery来检测新添加的复选框。希望它会对你有所帮助。

$(document).on('click','.checkBoxClass',function() {
        var selected = $('.checkBoxClass:enabled').length;
    if ($('.checkBoxClass:checked:enabled').length === selected) {
      $('#ckbCheckAll').prop('checked', true);
    } else {
      $('#ckbCheckAll').prop('checked', false);
    }
})

demo