ddxiami

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 2796|回复: 0

[Jquery] jQuery实现的全选、反选和不选功能

[复制链接]
发表于 2016-3-8 20:17:22 | 显示全部楼层 |阅读模式
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>演示:jQuery实现的全选、反选和不选功能</title>
<link rel="stylesheet" type="text/css" href="css/main.css" />
<style>
.demo{width:520px; margin:40px auto 0 auto; min-height:250px;}
ul li{line-height:30px; padding:4px 0; font-size:14px}
.btn{overflow: hidden;display:inline-block;*display:inline;padding:4px 20px 4px;font-size:14px;line-height:18px;*line-height:20px;color:#fff;text-align:center;vertical-align:middle;cursor:pointer;background-color:#5bb75b;border:1px solid #cccccc;border-color:#e6e6e6 #e6e6e6 #bfbfbf;border-bottom-color:#b3b3b3;-webkit-border-radius:4px;-moz-border-radius:4px;border-radius:4px; margin-left:2px}
</style>
<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
<script>  
$(function () {
        //全选或全不选
        $("#all").click(function(){   
            if(this.checked){   
                $("#list :checkbox").attr("checked", true);  
            }else{   
                        $("#list :checkbox").attr("checked", false);
            }   
        });
        //全选  
    $("#selectAll").click(function () {
         $("#list :checkbox,#all").attr("checked", true);  
    });  
        //全不选
    $("#unSelect").click(function () {  
         $("#list :checkbox,#all").attr("checked", false);  
    });  
    //反选
    $("#reverse").click(function () {
         $("#list :checkbox").each(function () {  
              $(this).attr("checked", !$(this).attr("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).attr("checked")==true){
                        chk++;
                }
    });
        if(chknum==chk){//全选
                $("#all").attr("checked",true);
        }else{//不全选
                $("#all").attr("checked",false);
        }
}
</script>
</head>

<body>


<div id="main">
   <h2 class="top_title"><a href="http://www.helloweba.com/view-blog-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>



</body>
</html>

js checkbox全选.rar

26.05 KB, 下载次数: 1273

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|技术文档库 ( 闽ICP备15017263号-2 )|网站地图

GMT+8, 2025-5-18 23:24 , Processed in 0.037816 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表