如何取消选中复选框,即使列表中的一个复选框未选中

How to uncheck a checkbox even if one among the list is unchecked?

本文关键字:复选框 一个 列表 何取消 取消      更新时间:2023-09-26

我正在尝试选中复选框(该复选框会选中列表中的所有其他复选框,即使列表中的一个复选框未选中,也会取消选中。现在只有第一个我的意思是检查列表中所有剩余复选框的一个复选框正在发生,反之亦然,我的意思是取消选中,即使列表中的一个未选中不起作用。我怎样才能做到这一点?

<label><input type="checkbox" name="sample" class="selectall"/> Select all</label>
<div id="checkboxlist">
<label><input type="checkbox" name="sample"/>checkbox1</label><br/>
<label><input type="checkbox" name="sample"/>checkbox2</label><br />
<label><input type="checkbox" name="sample"/>checkbox3</label><br />
<label><input type="checkbox" name="sample"/>checkbox4</label><br />
 </div> 

小提琴。

我会这样做:

$('.selectall').on('change', function(e) {
    var $inputs = $('#checkboxlist input[type=checkbox]');
    if(e.originalEvent === undefined) {
        var allChecked = true;
        $inputs.each(function(){
            allChecked = allChecked && this.checked;
        });
        this.checked = allChecked;
    } else {
        $inputs.prop('checked', this.checked);
    }
});
$('#checkboxlist input[type=checkbox]').on('change', function(){
    $('.selectall').trigger('change');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label><input type="checkbox" name="sample" class="selectall"/> Select all</label>
<div id="checkboxlist">
    <label><input type="checkbox" name="sample"/>checkbox1</label><br/>
    <label><input type="checkbox" name="sample"/>checkbox2</label><br />
    <label><input type="checkbox" name="sample"/>checkbox3</label><br />
    <label><input type="checkbox" name="sample"/>checkbox4</label><br />
    
</div>

编辑:根据A. Wolff的建议将e.isTrigger换成e.originalEvent === undefined,如下所述。

你可以像下面这样做。在 #checkboxlist 下为checkbox添加另一个点击事件,如果未选中checkbox,则取消选中.selectall

$('#checkboxlist input[type=checkbox]').click(function(){
   if (!$(this).is(':checked')) {
       $('.selectall').prop('checked', false);
   }
});

演示

查看此演示.. 与验证

<input type="checkbox" id="anchor-from"/>main
<input type="checkbox" class="checkall" id="period-daily" disabled />CheckBox2
<input type="checkbox" class="checkall" id="period-weekly"disabled />CheckBox3
<input type="checkbox" class="checkall" id="period-weekly2"disabled />CheckBox3
<input type="checkbox" class="checkall" id="period-weekly3"disabled />CheckBox3
<input type="checkbox" class="checkall" id="period-weekly4"disabled />CheckBox3

Js

$("#anchor-from").change(function(){
                        if($('#anchor-from').is(':checked'))
                        {
                          $(".checkall").attr("disabled", false); 
                          $(".checkall").prop("checked", true); 
                        }
                        else
                        {
                        $(".checkall").attr("disabled", true);
                         $(".checkall").prop("checked", false); 
                        }

                    });
                    $(".checkall").change(function(){
                    if($('.checkall').not(':checked'))
                    {
                          $(".checkall").attr("disabled", true); 
                          $(".checkall").prop("checked", false); 
                          $("#anchor-from").prop("checked", false); 
                    }
                 });
 $(document).ready(function () {
       $('input[name=sample]').click(function() {
           if ($(this).hasClass('selectall')) {
               if ($(this).prop('checked') == true) {
                   $('#checkboxlist input[name=sample]').prop('checked', true);
               }
               else {
                   $('#checkboxlist input[name=sample]').prop('checked', false);
               }
           }
           else {
               if ($('#checkboxlist input[name=sample]:checked').length == $('#checkboxlist input[name=sample]').length) {
                   $('.selectall').prop('checked', true)
               }
               else {
                   $('.selectall').prop('checked', false)
               }
           }
       });
   });