取消选择上面的复选框有问题(上面没有)

时间:2015-01-30 07:15:40

标签: jquery checkbox

您能否请一看这个演示,让我知道如何修复最后一个复选框的代码,以便在检查时取消选中以上所有复选框,并且在取消检查雕像时不执行任何操作?

<input id="selectAll" type="checkbox" name="slectAll">
<label for="selectAll">Yes, Select All</label>
<fieldset id="group_1">
   <input type="checkbox" name="numbers[]" value="0" />Item 1<br />
  <input type="checkbox" name="numbers[]" value="1" />Item 2<br />
  <input type="checkbox" name="numbers[]" value="2" />Item 3<br />
  <input type="checkbox" name="numbers[]" value="3" />Item 4<br />
  <input type="checkbox" name="numbers[]" value="4" />Item 5<br />
</fieldset>
<input id="planB" type="checkbox" name="planB">
<label for="planB">I am not Sure</label>

<script>
$(function() {
  var $all = $('input:checkbox[name=slectAll]').change(function() {
    $checks.prop("checked", this.checked);
  });

  var $checks = $('#group_1 :checkbox').change(function() {
    $all.prop("checked", $checks.not(':checked').length == 0);
  });

  var $none = $('input:checkbox[name=planB]').change(function() {
    $checks.prop("checked", this.checked);
  });    

 });    
</script>

由于

1 个答案:

答案 0 :(得分:1)

只需检查已检查状态

&#13;
&#13;
$(function() {
  var $all = $('input:checkbox[name=slectAll]').change(function() {
    $checks.prop("checked", this.checked);
    $none.prop('checked', false);
  });

  var $checks = $('#group_1 :checkbox').change(function() {
    $all.prop("checked", $checks.not(':checked').length == 0);
    $none.prop('checked', false);
  });

  var $none = $('input:checkbox[name=planB]').change(function() {
    if (this.checked) {
      $checks.add($all).prop("checked", false);
    }
  });

});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input id="selectAll" type="checkbox" name="slectAll"/>
<label for="selectAll">Yes, Select All</label>
<fieldset id="group_1">
  <input type="checkbox" name="numbers[]" value="0" />Item 1<br />
  <input type="checkbox" name="numbers[]" value="1" />Item 2<br />
  <input type="checkbox" name="numbers[]" value="2" />Item 3<br />
  <input type="checkbox" name="numbers[]" value="3" />Item 4<br />
  <input type="checkbox" name="numbers[]" value="4" />Item 5<br />
</fieldset>
<input id="planB" type="checkbox" name="planB"/>
<label for="planB">I am not Sure</label>
&#13;
&#13;
&#13;