所有复选框需要选中并取消选中主复选框 [英] all checkboxes need to be checked and unchecked with the main checkbox

查看:137
本文介绍了所有复选框需要选中并取消选中主复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以帮助我吗?

我有两个 div s。在一个 div 中,我有一个名为 allcheck 的复选框,在另一个 div ,我有名为 outcheck 的复选框。如果我检查多个,都需要检查,如果我取消选中多个,则全部都应该取消选中。输入广播是类 allcheck

  $('。inputradio' ).click(function(){

$(INPUT [name ='outCheck''))each(function(){
if(allCheck.checkbox == true){ //复选检查
this.checked = true;
} else {
this.checked = false;
}
});
} );

HTML



这是<$

 < div id =actions>< c $ c> div  ; div id =box> 
< ul>< li class =inputradio>< input name =allChecktype =checkbox>< / li>
< li class =multiple>< a href =#class =bt btleft>多个< / a>< / li>

这是针对子复选框的:

 < ul>< li id =outcheckbox>< input name =outChecktype =checkbox>< / li> 

即使选中了一个孩子,如果我们选中主复选框,也需要选中所有孩子。我发布的代码只是反过来检查。

解决方案

试试这个 -

  $('input [name = allCheck]')。click(function(){
$(INPUT [name ='outCheck'])。prop 'checked',this.checked);
});

这将切换所有名称 outCheck 点击 allCheck



请参阅 demo






UPDATE



查看已更新的演示



这可以按预期工作。如果所有复选框都被手动选中,那么主复选框也会发生变化,反之亦然,如果一个孩子未被选中,主复选框也会被取消选中。


Can anyone help me with this?

I have two divs. In one div, I have a checkbox named allcheck, and in the other other div, I have checkboxes with name outcheck. If I check multiple, all need to be checked, and if I uncheck multiple, all should be unchecked. input radio is class of allcheck.

$('.inputradio').click(function(){

    $("INPUT[name='outCheck']").each(function () {
        if (allCheck.checkbox== true) {//multi check is checked
            this.checked = true;
        } else {
            this.checked = false;
        }
    });
});

HTML

This is the div for main checkbox

<div id="actions"><div id="box">
<ul><li class="inputradio"><input name="allCheck" type="checkbox" ></li>
<li class="multiple"><a href="#" class="bt btleft">Multiple</a></li>

This is for child checkboxes:

<ul><li id="outcheckbox"><input name="outCheck" type="checkbox"></li>

Even if one child is checked, if we check the main checkbox, all need to be selected. The code I posted is just inverting the check.

解决方案

Try this -

$('input[name=allCheck]').click(function(){
   $("INPUT[name='outCheck']").prop('checked', this.checked);
});

This will toggle all checkboxes having name outCheck on click of allCheck.

See demo


UPDATE

see updated demo here

This works as expected. If all checkboxes are manually checked then main checkbox also changes and vice versa if one child is unchecked, main checkbox also gets unchecked.

这篇关于所有复选框需要选中并取消选中主复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆