简体   繁体   English

许多复选框应始终保持未选中状态

[英]One checkbox of many should always stay unchecked

There are a lot of topics about JavaScript and conditional checking checkboxes, but I have not found one that matches my case. 关于JavaScript和条件检查复选框,有很多主题,但是我没有找到与我的情况相符的主题。

My use case: I have generated a number of rows with checkboxes each row, let's say the number is n . 我的用例:我已经生成了许多行,每行都有复选框,比方说,数目为n Now the user should be able to check only n-1 of them. 现在,用户应该只能检查其中的n-1个。 So in other words when we have 4 checkboxes and 3 of 4 are checked, the last one should be disabled. 因此,换句话说,当我们有4个复选框并选中4个中的3个时,应禁用最后一个。 When only 2 of 4 are checked then all checkboxes are available. 如果仅选中4个中的2个,则所有复选框均可用。

I have tried: 我努力了:

var $checks = $('input:checkbox').click(function (e) {
        var numChecked = $checks.filter(':checked').length;
        var lastUnchecked = $checks.prop('checked', false);

        if (numChecked = $checks.length - 1) {
            lastUnchecked.disabled = true;
        }
        else {
            //enable checkbox that was disabled before
            lastUnchecked.disabled = false;
        }
    });

But this doesn't work because var lastUnchecked = $checks.prop('checked', false); 但这是行不通的,因为var lastUnchecked = $checks.prop('checked', false); return all checkboxes, not only unchecked 返回所有复选框,不仅未选中

Try counting the checked items each time user checkes one of them: 每次用户检查其中一项时,请尝试计算已检查项目:

 $(document).on('change', '.chk', function() { var that = $(this); var par = that.closest('ul'); var max = par.find('.chk').length - 1; var checked = par.find('.chk').filter(':checked').length; if (checked < max) { par.find('.chk').prop('disabled', ''); } else { par.find('.chk').not(':checked').prop('disabled', 'disabled'); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <ul> <li><input class="chk" type="checkbox" /> Checkbox</li> <li><input class="chk" type="checkbox" /> Checkbox</li> <li><input class="chk" type="checkbox" /> Checkbox</li> <li><input class="chk" type="checkbox" /> Checkbox</li> <li><input class="chk" type="checkbox" /> Checkbox</li> </ul> 

Try this. 尝试这个。

Logic: on change, first re/enable all checkboxes. 逻辑:更改时,首先重新/启用所有复选框。 Then figure out afresh if we need to disable any, based on how many are checked. 然后根据要检查的数量重新确定是否需要禁用任何功能。

var table = $('table'), cbs = $(':checkbox');
table.on('change', ':checkbox', function(evt) {
    cbs.prop('disabled', false);
    if (cbs.filter(':checked').length == cbs.length - 1) 
        cbs.not(':checked').prop('disabled', 1);
})

Fiddle . 小提琴

This is simpler 这比较简单

$(document).ready(function(){
  var numBox;

  var $checks = $('input:checkbox').click(function (e) {
      var numChecked = $checks.filter(':checked').length;

      if (numChecked < numBox - 1) {
        $checks.filter(':disabled').prop('disabled', false);
      } else {
        $checks.not(':checked').prop('disabled', true);
      }
  });

  numBox = $check.length;
});

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

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