jq 父级选择时,子级全选;父级取消全选,子级全部取消

 html代码:

<volist name="sblx.shebei" id="sb">
<tr>

<td>
    <input level='1' name="shebei[]" type="checkbox" value="<{$sb.sbid}>"><{$sb.sbname}>
</td>
<td>
    <volist name="sb.xjshebei" id="xjsb">
    <input level='2' name="shebei[]" type="checkbox" value="<{$xjsb.sbid}>"><{$xjsb.sbname}>
    </volist>
</td>

</tr>
</volist>

jq代码:

    $(function(){

        $('[level=1]').click(function(){
            if(this.checked)
            {
                $(this).parent().parent().find('input').prop('checked','checked');
            }
            else
            {
                $(this).parent().parent().find('input').removeAttr('checked');
            }
        });

        $('[level=2]').click(function(){
            if (this.checked)
            {
                $(this).parent().prev().find('input').prop('checked','checked');
            }
            else
            {
                //当子分类个数为0时,去掉父级选项
                if($(this).parent().find('input:checked').length==0)
                {
                    $(this).parent().find('input').removeAttr('checked');
                    $(this).parent().prev().find('input').removeAttr('checked');
                }       
            }
        });
    });

父级选择时,子级全选;父级取消全选,子级全部取消;

子级点击时,父级选择;子级个数为0时,父级取消选择。

 

代码摘自他人,供本人学习记录而已。

转载于:https://my.oschina.net/u/659587/blog/1846277

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值