checkbox全选与反选

1.只有一组checkbox的时候:


<html lang="zh-CN">

<head runat="server">
<meta charset="utf-8">
<title>阿斯达是</title>
<script src="http://code.jquery.com/jquery-1.4.4.min.js" type="text/javascript"></script>    <script type="text/javascript">
        $(function() {
           $("#checkAll").click(function() {
                $('input[name="subBox"]').attr("checked",this.checked); 
            });
            var $subBox = $("input[name='subBox']");
            $subBox.click(function(){
                $("#checkAll").attr("checked",$subBox.length == $("input[name='subBox']:checked").length ? true : false);
            });

        });


//点击子类的另一种

//点击子类
$('.checkall').parent().next().children().children().click(function(){
var checkname=$(this).attr('name');
var subBox = $("input[name="+checkname+"]");
$(this).parent().parent().prev().children().prop("checked",subBox.length == $("input[name="+checkname+"]:checked").length ? true : false);
})

    </script>
 
</head>
<body>
    <div>
        <input id="checkAll" type="checkbox">全选
        <input name="subBox" type="checkbox">阿斯达斯
        <input name="subBox" type="checkbox">阿三的发生
        <input name="subBox" type="checkbox">阿斯达
        <input name="subBox" type="checkbox">阿斯达斯
    </div>


</body>

</html>



2.多组input的时候

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
<title>表1</title>


<!-- Bootstrap -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
</head>
<body>

<div class="table-responsive">
<table id="customers" class="table text-center table-bordered tb-striped">
<thead>
<tr>
<th>大类</th>
<th>模块</th>
<th>功能点</th>
</tr>
</thead>
<tbody>
<!-- 功能一 -->
<tr>
<td class="bg-white" rowspan="4">基本功能</td>
<td><input type="checkbox" class="checkall" name="funlog"/>登录注册</td>
<td>
<label><input type="checkbox" class="sub" name="funlog" />功能1</label>
<label><input type="checkbox" class="sub" name="funlog" />功能2</label>
<label><input type="checkbox" class="sub" name="funlog" />功能3</label>
<label><input type="checkbox" class="sub" name="funlog" />功能4</label>
<label><input type="checkbox" class="sub" name="funlog" />功能5</label>
<label><input type="checkbox" class="sub" name="funlog" />功能6</label>
</td>
</tr>
<tr>
<td><input type="checkbox"  name="funuser"  class="checkall" />用户中心</td>
<td>
<label><input type="checkbox" name="funuser" />功能1</label>
<label><input type="checkbox" name="funuser" />功能2</label>
<label><input type="checkbox" name="funuser" />功能3</label>
<label><input type="checkbox" name="funuser" />功能4</label>
<label><input type="checkbox" name="funuser" />功能5</label>
<label><input type="checkbox" name="funuser" />功能6</label>
</td>
</tr>
<tr>
<td><input type="checkbox" name="funlog2" class="checkall"/>第三方登录</td>
<td>
<label><input type="checkbox" name="funlog2" />功能1</label>
<label><input type="checkbox" name="funlog2" />功能2</label>
<label><input type="checkbox" name="funlog2" />功能3</label>
<label><input type="checkbox" name="funlog2" />功能4</label>
<label><input type="checkbox" name="funlog2" />功能5</label>
<label><input type="checkbox" name="funlog2" />功能6</label>
</td>
</tr>
<tr>
<td><input type="checkbox"  name="fundis" class="checkall" />内容展示</td>
<td>
<label><input type="checkbox" name="fundis" />功能1</label>
<label><input type="checkbox" name="fundis" />功能2</label>
<label><input type="checkbox" name="fundis" />功能3</label>
<label><input type="checkbox" name="fundis" />功能4</label>
<label><input type="checkbox" name="fundis" />功能5</label>
<label><input type="checkbox" name="fundis" />功能6</label>
</td>
</tr>

</table>
</div>
<script src="http://cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>
<script>
var a=0;

//checkbox全选
$('.checkall').click(function(){
if($(this).is(':checked')){
$(this).parent().next().children().each(function(){
$(this).children().prop("checked","checked");
})
a = $(this).parent().next().children().length;
}
else {
$(this).parent().next().children().each(function(){
$(this).children().removeAttr("checked");
})
a = 0;
}
})

//点击子类时,判断是否是全选
$('.sub').click(function(){
b = $(this).parent().siblings().length //
if($(this).is(':checked')){
a++;
}else{
a--;
}

if(a-1 == b){

$(this).parents('td').prev().children().prop('checked','checked')//prop()方法是当attr用的时候是起作用但是web页面没效果的时候用
}else{
$(this).parents('td').prev().children().removeAttr('checked')
}

})
</script>

</body>

</html>


attr和prop使用情况


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值