已经选中的复选框要取消选中并取消选中要在jQuery

时间:2015-08-26 06:19:38

标签: jquery html

我有一个主复选框,下面还有一组复选框,其中一些已经检查过,其中一些未选中。我的查询是如果我将检查主复选框,然后已检查复选框要取消选中并取消选中要检查的复选框。

例如: enter image description here

3 个答案:

答案 0 :(得分:0)

根据您的查询

  

如果我将检查主复选框,则已检查复选框要取消选中并取消选中要检查的复选框

试试吧,它会起作用



  $('#master').on('click', function() {
 if($(this).is(':checked')){
      $('.branch').trigger('click');
    }             
 });

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="checkbox" id="master">master</br>
<input type="checkbox" class="branch">1
<input type="checkbox" class="branch">2
<input type="checkbox" class="branch" checked>3
<input type="checkbox" class="branch">4
<input type="checkbox" class="branch">5
<input type="checkbox" class="branch" checked>6
<input type="checkbox" class="branch">7
<input type="checkbox" class="branch">8
<input type="checkbox" class="branch" checked>9
&#13;
&#13;
&#13;

答案 1 :(得分:0)

如果不触发所有输入,请尝试以下操作:

var masterCheckbox = jQuery("#master");
            
masterCheckbox.on("change", function()
{
  if(masterCheckbox.is(":checked"))
  {
    var checkboxes = jQuery(".myCheckbox");

    //get all checked and unchecked
    var checked = checkboxes.filter(":checked");
    var notchecked = checkboxes.filter(":not(:checked)");

    checked.prop( "checked", false );

    notchecked.prop( "checked", true );
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="checkbox" id="master" />
<br>
<input type="checkbox" class="myCheckbox" checked />
<input type="checkbox" class="myCheckbox" />
<input type="checkbox" class="myCheckbox" checked />
<input type="checkbox"  class="myCheckbox" />

答案 2 :(得分:0)

使用prop()方法

&#13;
&#13;
$('#master').on('click', function() {  
    $('.branch').each(function(){
        $(this).prop('checked',!$(this).prop('checked'))
    });
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="checkbox" id="master">master</br>
<input type="checkbox" class="branch">1
<input type="checkbox" class="branch">2
<input type="checkbox" class="branch" checked>3
<input type="checkbox" class="branch">4
<input type="checkbox" class="branch">5
<input type="checkbox" class="branch" checked>6
<input type="checkbox" class="branch">7
<input type="checkbox" class="branch">8
<input type="checkbox" class="branch" checked>9
&#13;
&#13;
&#13;