【特效】复选框的全选、全不选、反选操作

顾名思义。

朴素效果截图:

 

html

<div class="con">

         <h1>复选框的全选、全不选、反选操作</h1>

   

         <div class="box">

             <h2>全选/全不选:</h2>

             <p><input type="checkbox" class="check_kid"> 选项一</p>

        <p><input type="checkbox" class="check_kid"> 选项二</p>

        <p><input type="checkbox" class="check_kid"> 选项三</p>

        <p><input type="checkbox" class="check_kid"> 选项四</p>

        <p><input type="checkbox" class="check_all"> 全选/全不选</p>                       

    </div>

   

    <div class="box">

             <h2>反选:</h2>

             <p><input type="checkbox" class="check_kid_2"> 选项一</p>

        <p><input type="checkbox" class="check_kid_2"> 选项二</p>

        <p><input type="checkbox" class="check_kid_2"> 选项三</p>

        <p><input type="checkbox" class="check_kid_2"> 选项四</p>

        <p><input type="button" value="反选" class="check_other"></p>                      

    </div>

   

    <div class="box">

             <h2>一行有多个:</h2>

             <table class="check_table">

                 <tr>

                     <td><input type="checkbox" class="check_line" name="check_1"> 全选1行</td>

                <td><input type="checkbox" class="check_one" name="check_1"> 选项11</td>

                <td><input type="checkbox" class="check_one" name="check_1"> 选项12</td>

                <td><input type="checkbox" class="check_one" name="check_1"> 选项13</td>

            </tr>

            <tr>

                     <td><input type="checkbox" class="check_line" name="check_1"> 全选2行</td>

                <td><input type="checkbox" class="check_one" name="check_1"> 选项21</td>

                <td><input type="checkbox" class="check_one" name="check_1"> 选项22</td>

                <td><input type="checkbox" class="check_one" name="check_1"> 选项23</td>

            </tr>

            <tr>

                     <td colspan="4"><input type="checkbox" class="check_all_2"> 全选/全不选</td>

            </tr>

        </table>                               

    </div>

    <br><br><br> 

</div>

 

css

.con{ width:1000px; margin:0 auto;}

.con h1{ text-align:center; font-weight:normal;}

h1,h2{ font-weight:normal; color:#0CC;}

ul{ margin:0; padding:0; list-style:none;}

 

.box{ border:1px solid #0C9; padding:20px; margin:20px;}

.check_table{ width:100%; border-collapse:collapse; border-spacing:0;}

.check_table td{ border:1px solid #ccc; height:50px; padding:0 10px;}

 

js

$(document).ready(function(){

        

//单一控制全选

         $(".check_kid").click(function(){

                   var num1=$(".check_kid").length;

                   var num2=$(".check_kid:checked").length;                               

                   if(num1==num2)               

                    {

                            $(".check_all").prop("checked",true);

                            }

                   else{

                            $(".check_all").prop("checked",false);

                            }       

                   });

                           

         $(".check_all").click(function(){                

                   $(".check_kid").prop("checked",this.checked);                                           

                   });    

                  

//反选              

         $(".check_other").click(function(){

                   $(".check_kid_2").each(function(){

                            this.checked=!this.checked;

                            });

                   });    

                  

//控制本行              

         $(".check_one").click(function(){

                   var check_tr=$(this).closest("tr");

                   var num11=$(".check_one",check_tr).length;

                   var num22=$(".check_one:checked",check_tr).length;         

                   var check_line=$(this).parents('tr').find(".check_line");                  

                   if(num11==num22){

                            check_line.prop("checked",true);

                            }

                   else{

                            check_line.prop("checked",false);

                            }       

                   });    

                           

         $(".check_line").click(function(){

                    check_one=$(this).parents('tr').find(".check_one");   

                    check_one.prop("checked",this.checked);  

                    });

                    

//控制所有

         $("[name='check_1']").click(function(){

                   var num111=$("[name='check_1']").length;

                   var num222=$("[name='check_1']:checked").length;             

                   if(num111==num222){

                            $(".check_all_2").prop("checked",true);                           

                            }                

                   else{

                            $(".check_all_2").prop("checked",false);                          

                            }

                   });    

                           

         $(".check_all_2").click(function(){           

                   $("[name='check_1']").prop("checked",this.checked);                                       

                   });                       

                           

}); 

 

效果预览:http://www.gbtags.com/gb/rtreplayerpreview-standalone/2855.htm

源码下载:http://pan.baidu.com/s/1kUIG8cB

 

转载于:https://www.cnblogs.com/xiaoxianweb/p/5509079.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值