【Jquery】------ Jquery实现Checkbox多选并获取值,选中全部,取消全部的实例代码展示

16 篇文章 0 订阅
16 篇文章 0 订阅

1.Jquery实现Checkbox多选,选中全部,取消全部的实例代码展示

1.1 展示效果图:

2.2 css 样式: 

<style>
       
        .bottom-block-layer-div{
            left: 48rem;
            width: 60%;
            color: #fff;
            display: inline-flex;
            position: fixed;
            bottom: 2px;
            z-index: 999;
        }
        .bottom-block-layer-li{
            width: 120px;
            box-shadow: inset 0 0 0px 0px #0081ff;
            background-color: rgba(0,3,7,0.3);
            border-left: 2px solid #072e7d;
            margin-left: 5px;
            font-size: 12px;
            text-align: center;
            font-size: 0.2rem;
    
        }
        .bottom-block-layer-li li:nth-of-type(1){
            text-align: left;
            margin: 5px 5px 5px 4px;
            padding: 5px 5px 5px 0px;
        }
        .bottom-block-layer-li li:nth-of-type(2){
            background-color: #093c70;
            padding: 8px;
            text-align: left;
            padding-left: 21px;
            color: white;
            font-weight: bold;
        }
        .bottom-block-layer-li li,.bottom-block-layer-title li{
            list-style-type: none;
        }
        .bottom-block-layer-li li:nth-of-type(1) span{
            position: absolute;
            top: 12px;
            margin-left: 4px;
        }
        .bottom-block-layer-li li:nth-of-type(1) input{
            margin-left: 2px;
        }
        .bottom-block-layer-li li:nth-of-type(2) span{
            margin-right:5px;
        }
        .bottom-block-layer-title{
            font-size: 12px;
            padding: 8px;
            background-color: rgb(9 60 112);
            box-shadow: inset 0 0 0px 0px #00bcd4;
            margin-left: 5px;
            text-align: center;
        }
    </style>

2.2 HTML 代码: 

<!--图层-->
<div class="bottom-block-layer-div" style="z-index: 10;">
    <div class="bottom-block-layer-title">
        <li>图</li>
        <li>层</li>
    </div>
    <div class="bottom-block-layer-title">
        <li><input type="checkbox" name="" id="allCheck"  value="" style="position: relative;left: 0px;top:2px;"></li>
        <li>全</li>
        <li>部</li>
    </div>
    <div class="bottom-block-layer-li">
        <li><input type="checkbox" class="input-checkbox-span"  name="" value="金毛犬"><span>金毛犬</span></li>
        <li><span>数量</span><span>12</span></li>
    </div>
    <div class="bottom-block-layer-li">
        <li><input type="checkbox" class="input-checkbox-span"  name="" value="泰迪犬"><span>泰迪犬</span></li>
        <li><span>数量</span><span>12</span></li>
        </div>
    <div class="bottom-block-layer-li">
        <li><input type="checkbox" class="input-checkbox-span"  name="" value="阿拉斯加犬"><span>阿拉斯加犬</span></li>
        <li><span>数量</span><span>212</span></li>
    </div>
    <div class="bottom-block-layer-li">
        <li><input type="checkbox" class="input-checkbox-span"  name="" value="柴犬"><span>柴犬</span></li>
        <li><span>数量</span><span>112</span></li>
    </div>
</div>

3.3 Jquery 代码: 

 //checkbox
       var selAll = document.getElementById('allCheck'); // 全选
       var selItem = document.getElementsByClassName("input-checkbox-span");// 单个选项
       var checkedArray=[];//选中数据

        //移除取消选中值
        function fnDeleteCancelValue(value){
         return checkedArray=checkedArray.filter(function(val,index){
                if(val!=value){
                    return val;
                }
            }) 
        }

        //监听checkbox选中
        $(".input-checkbox-span").off("click").on("click",function(){
               var isChecked=this.checked;//得到当前checkbox状态
               var getCheckedValue=this.value;//得到当前选中值
               var checkboxCount=selItem.length;//选项总数
               var checkedCount=$(".input-checkbox-span:checked").length;//选中选项总数
               if(checkedCount==checkboxCount){
                  selAll.checked = true;
               }else{
                  selAll.checked = false;
               }
               if(isChecked){
                   console.log("当前是选中:"+getCheckedValue);
                   checkedArray.push(getCheckedValue);    
               }else{
                   console.log("当前是取消:"+getCheckedValue);
                   fnDeleteCancelValue(getCheckedValue);
               }
               console.log(checkedArray);
        });
       
        //监听全部
        function fnCheckedAll(){
           var ischeck = selAll.checked;
           checkedArray=[];//清空
           for(var i = 0;i<selItem.length;i++){
               selItem[i].checked = (ischeck ? true : false);
               if(ischeck){
                 checkedArray.push(selItem[i].value);
               }
           }
           console.log("当前是:"+(ischeck?"选中全部":"取消全部"));
           console.log(checkedArray);
        }

        //监听全部
        $("#allCheck").click(function (e){
           fnCheckedAll();
        });

4.4 全部代码实例:

<!DOCTYPE html>
<html lang="en">

<head>
    <!-- Use correct character set. -->
    <meta charset="utf-8">
    <!-- Tell IE to use the latest, best version. -->
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <!-- Make the application on mobile take up the full browser screen and disable user scaling. -->
    <meta name="viewport"
        content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
    <title></title>
    <style>
    
        html,
        body {
            width: 100%;
            height: 100%;
            margin: 0;
            padding: 0;
            overflow: hidden;
        }
    </style>

    <style>
       
        .bottom-block-layer-div{
            left: 48rem;
            width: 60%;
            color: #fff;
            display: inline-flex;
            position: fixed;
            bottom: 2px;
            z-index: 999;
        }
        .bottom-block-layer-li{
            width: 120px;
            box-shadow: inset 0 0 0px 0px #0081ff;
            background-color: rgba(0,3,7,0.3);
            border-left: 2px solid #072e7d;
            margin-left: 5px;
            font-size: 12px;
            text-align: center;
            font-size: 0.2rem;
    
        }
        .bottom-block-layer-li li:nth-of-type(1){
            text-align: left;
            margin: 5px 5px 5px 4px;
            padding: 5px 5px 5px 0px;
        }
        .bottom-block-layer-li li:nth-of-type(2){
            background-color: #093c70;
            padding: 8px;
            text-align: left;
            padding-left: 21px;
            color: white;
            font-weight: bold;
        }
        .bottom-block-layer-li li,.bottom-block-layer-title li{
            list-style-type: none;
        }
        .bottom-block-layer-li li:nth-of-type(1) span{
            position: absolute;
            top: 12px;
            margin-left: 4px;
        }
        .bottom-block-layer-li li:nth-of-type(1) input{
            margin-left: 2px;
        }
        .bottom-block-layer-li li:nth-of-type(2) span{
            margin-right:5px;
        }
        .bottom-block-layer-title{
            font-size: 12px;
            padding: 8px;
            background-color: rgb(9 60 112);
            box-shadow: inset 0 0 0px 0px #00bcd4;
            margin-left: 5px;
            text-align: center;
        }
    </style>
</head>

<body>
<!--图层-->
<div class="bottom-block-layer-div" style="z-index: 10;">
    <div class="bottom-block-layer-title">
        <li>图</li>
        <li>层</li>
    </div>
    <div class="bottom-block-layer-title">
        <li><input type="checkbox" name="" id="allCheck"  value="" style="position: relative;left: 0px;top:2px;"></li>
        <li>全</li>
        <li>部</li>
    </div>
    <div class="bottom-block-layer-li">
        <li><input type="checkbox" class="input-checkbox-span"  name="" value="金毛犬"><span>金毛犬</span></li>
        <li><span>数量</span><span>12</span></li>
    </div>
    <div class="bottom-block-layer-li">
        <li><input type="checkbox" class="input-checkbox-span"  name="" value="泰迪犬"><span>泰迪犬</span></li>
        <li><span>数量</span><span>12</span></li>
        </div>
    <div class="bottom-block-layer-li">
        <li><input type="checkbox" class="input-checkbox-span"  name="" value="阿拉斯加犬"><span>阿拉斯加犬</span></li>
        <li><span>数量</span><span>212</span></li>
    </div>
    <div class="bottom-block-layer-li">
        <li><input type="checkbox" class="input-checkbox-span"  name="" value="柴犬"><span>柴犬</span></li>
        <li><span>数量</span><span>112</span></li>
    </div>
</div>
<script src="jquery-3.3.1.min.js"></script>
<script >
       //checkbox
       var selAll = document.getElementById('allCheck'); // 全选
       var selItem = document.getElementsByClassName("input-checkbox-span");// 单个选项
       var checkedArray=[];//选中数据

        //移除取消选中值
        function fnDeleteCancelValue(value){
         return checkedArray=checkedArray.filter(function(val,index){
                if(val!=value){
                    return val;
                }
            }) 
        }

        //监听checkbox选中
        $(".input-checkbox-span").off("click").on("click",function(){
               var isChecked=this.checked;//得到当前checkbox状态
               var getCheckedValue=this.value;//得到当前选中值
               var checkboxCount=selItem.length;//选项总数
               var checkedCount=$(".input-checkbox-span:checked").length;//选中选项总数
               if(checkedCount==checkboxCount){
                  selAll.checked = true;
               }else{
                  selAll.checked = false;
               }
               if(isChecked){
                   console.log("当前是选中:"+getCheckedValue);
                   checkedArray.push(getCheckedValue);    
               }else{
                   console.log("当前是取消:"+getCheckedValue);
                   fnDeleteCancelValue(getCheckedValue);
               }
               console.log(checkedArray);
        });
       
        //监听全部
        function fnCheckedAll(){
           var ischeck = selAll.checked;
           checkedArray=[];//清空
           for(var i = 0;i<selItem.length;i++){
               selItem[i].checked = (ischeck ? true : false);
               if(ischeck){
                 checkedArray.push(selItem[i].value);
               }
           }
           console.log("当前是:"+(ischeck?"选中全部":"取消全部"));
           console.log(checkedArray);
        }

        //监听全部
        $("#allCheck").click(function (e){
           fnCheckedAll();
        });
    
</script>

</body>

</html>

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

皮皮冰要做大神

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值