JQuery - 复选框 - 全选和全不选

<!DOCTYPE html>
<html>
<head >
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>jquery操作checkbox方法 - 全选和取消全选</title>
    <script type="text/javascript" src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
    <script type="text/javascript" >
        function hehe() {
            var isChecked = $('#haha').prop("checked");
            console.log('isChecked = ', isChecked)

            $("#haha").each(function() {
                console.log('this.checked = ', this.checked)

                if (this.checked) {
                    console.log('------ add ')
                    selectAll()
                    console.log('------ add end')

                } else {
                    console.log('------ remove')
                    selectNone()
                    console.log('------ remove end')


                }
            })
        }


        // function hehe() {
        //     var isChecked = $('#haha').prop("checked");
        //     console.log('isChecked = ', isChecked)
        //
        //     $("#haha").each(function() {
        //         console.log('this.checked = ', this.checked)
        //
        //         if (this.checked) {
        //             console.log('------ add ')
        //             selectAll()
        //             console.log('------ add end')
        //
        //         } else {
        //             console.log('------ remove')
        //             selectNone()
        //             console.log('------ remove end')
        //
        //
        //         }
        //     })
        // }
        //全选
        function selectAll() {
            //方法一:
            // $("input[name='bjjb']").attr("checked",true);
            //方法二:
            // $("input[name='bjjb']").each(function(){
            //     $(this).attr("checked",true);
            // });
            $("input[name='bjjb']").each(function(){
                $(this).prop("checked",true);
            });
            console.log('------ 全不选')
            $("#s-a").html("全不选");
        }
        //取消全选
        function selectNone(){
            //方法一:
            // $("input[name='bjjb']").removeAttr("checked");
            //方法二:
            // $("input[name='bjjb']").each(function(){
            //     $(this).attr("checked",false);
            // });
            $("input[name='bjjb']").each(function(){
                $(this).prop("checked",false);
            });
            console.log('------ 全选')
            $("#s-a").html("全选");
        }
        //反选
        // function selectInvert() {
        //     $("input[name='bjjb']").each(function(index,item){
        //         if ($(this).attr("checked")) {
        //             $(this).removeAttr("checked");
        //         } else {
        //             $(this).attr("checked", true);
        //         }
        //     });
        // }
        // function selectOne() {
        //     var checked=$("input[name='bjjb']:checked");
        //     if(checked.length==0){
        //         alert("请至少选择一个");
        //         return ;
        //     }
        //     $("#checked").html("");
        //     $("#checkedText").html("");
        //     $("input[name='bjjb']:checked").each(function () {
        //         $("#checked").append($(this).val()+",");
        //         //注意文本一定要元素标签如span否则next得不到值
        //         $("#checkedText").append($(this).next().text()+",");
        //     });
        //
        // }


    </script>
</head>
<body>
<form  id="" action="" method="post">
    <div >
        <input type="checkbox" name="bjjb" value="1"/><span>交通事故</span></br>
        <input type="checkbox" name="bjjb" value="2"/><span>自然灾害</span></br>
        <input type="checkbox" name="bjjb" value="3"/><span>恶劣天气</span></br>
        <input type="checkbox" name="bjjb" value="4"/><span>严重违法行为</span></br>
        <input type="checkbox" name="bjjb" value="5"/><span>路面损毁</span></br>
    </div>
    <div style="margin-top:10px;">
        <input type="checkbox" id="haha" name="selectAll" onclick="hehe()"   value="全选" /> <div id="s-a">全选</div>
        <!--        <input type="button"   onclick="selectNone()"   value="全不选" />-->

    </div>

</form>
</body>
</html>

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值