多选框用法

https://www.helloweba.net/demo/selectall/
演示地址
<script>  
$(function () {
    //全选或全不选
    $("#all").click(function(){   
        if(this.checked){   
            $("#list :checkbox").prop("checked", true);  
        }else{   
            $("#list :checkbox").prop("checked", false);
        }   
     }); 
    //全选  
    $("#selectAll").click(function () {
         $("#list :checkbox,#all").prop("checked", true);  
    });  
    //全不选
    $("#unSelect").click(function () {  
         $("#list :checkbox,#all").prop("checked", false);  
    });  
    //反选 
    $("#reverse").click(function () { 
         $("#list :checkbox").each(function () {  
              $(this).prop("checked", !$(this).prop("checked"));  
         });
         allchk();
    });
    
    //设置全选复选框
    $("#list :checkbox").click(function(){
        allchk();
    });
 
    //获取选中选项的值
    $("#getValue").click(function(){
        var valArr = new Array;
        $("#list :checkbox[checked]").each(function(i){
            valArr[i] = $(this).val();
        });
        var vals = valArr.join(',');
          alert(vals);
    });
}); 
function allchk(){
    var chknum = $("#list :checkbox").size();//选项总个数
    var chk = 0;
    $("#list :checkbox").each(function () {  
        if($(this).prop("checked")==true){
            chk++;
        }
    });
    if(chknum==chk){//全选
        $("#all").prop("checked",true);
    }else{//不全选
        $("#all").prop("checked",false);
    }
}
</script> 
</head>

<body>
<div id="header">
   <div id="logo"><h1><a href="http://www.helloweba.net" title="返回helloweba首页">helloweba</a></h1></div>
   <div class="demo_topad"><script src="/js/ad_js/demo_topad.js" type="text/javascript"></script></div>
</div>

<div id="main">
   <h2 class="top_title"><a href="http://www.helloweba.net/javascript/254.html">jQuery实现的全选、反选和不选功能</a></h2>
   <div class="demo">
           <ul id="list">  
            <li><label><input type="checkbox" value="1"> 1.时间都去哪儿了</label></li>
            <li><label><input type="checkbox" value="2"> 2.海阔天空</label></li>
            <li><label><input type="checkbox" value="3"> 3.真的爱你</label></li>
            <li><label><input type="checkbox" value="4"> 4.不再犹豫</label></li>
            <li><label><input type="checkbox" value="5"> 5.光辉岁月</label></li>
            <li><label><input type="checkbox" value="6"> 6.喜欢妳</label></li>
        </ul>
          <input type="checkbox" id="all">
        <input type="button" value="全选" class="btn" id="selectAll">  
        <input type="button" value="全不选" class="btn" id="unSelect">  
        <input type="button" value="反选" class="btn" id="reverse">  
        <input type="button" value="获得选中的所有值" class="btn" id="getValue">  
    </div>
 <br/><div class="ad_76090"><script src="/js/ad_js/bd_76090.js" type="text/javascript"></script></div><br/>
</div>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值