js全选、全不选功能

实现功能:

1. 全选

2. 全不选

3. 每条数据被勾选后,全选框变成被勾选状态

4. 勾选全选框,取消某条数据的勾选,全选框变成未勾选状态

直接看demo:

(label标签和类名ui-checkbox、ui-checkbox-inline是在checkbox.css中对复选框做了美化)

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<link rel="stylesheet" href="checkbox.css">
<script src="https://cdn.bootcss.com/jquery/1.10.2/jquery.min.js"></script>
<style> 
*{padding:0;margin:0;}
</style>
<body>
<div class="outbox" style="margin-bottom: 60px;">
	<!-- 复选 & 单选 -->
	<div class="ui-checkbox ui-checkbox-inline">
        <input type="checkbox" class="checkbox all_select" 
               id="check_alls" onclick="marked(this)">
        <label for="check_alls">全选</label>
	</div>
	<div class="ui-checkbox ui-checkbox-inline">
        <input type="checkbox" class="checkbox other_select"
               id="check_other" onclick="marked(this)">
        <label for="check_other">反选</label>
	</div>
	<div>
		<div class="ui-checkbox ui-checkbox-inline">
            <input type="checkbox" class="check_item" 
                   id="first_item" value="first_item" onclick="marked(this)">
	        <label for="first_item">1</label>
		</div>
		<div class="ui-checkbox ui-checkbox-inline">
            <input type="checkbox" class="check_item" 
                   id="second_item" value="second_item" onclick="marked(this)">
	        <label for="second_item">2</label>
		</div>
		<div class="ui-checkbox ui-checkbox-inline">
            <input type="checkbox" class="check_item" 
                   id="third_item" value="third_item" onclick="marked(this)">
	        <label for="third_item">3</label>
		</div>
		<div class="ui-checkbox ui-checkbox-inline">
            <input type="checkbox" class="check_item" 
                   id="forth_item" value="forth_item" onclick="marked(this)">
	        <label for="forth_item">4</label>
		</div>
	</div>
</div>
</body>
<script>
    function marked(obj){
    	// 点击全选的复选框
    	if(obj.id == 'check_alls'){
    		// 如果全选按钮是选中状态
    		if(jQuery('#check_alls').is(':checked')){
    			jQuery('.check_item').prop('checked',true);
    		}else{
    			jQuery('.check_item').prop('checked',false);
    		}
    	}

    	// 点击反选的复选框
    	if(obj.id == 'check_other'){
    		if(jQuery('#check_other').is(':checked')){
    			jQuery('.check_item').each(function(index,val){
					if(jQuery(this).is(":checked")){
		    			jQuery(this).prop('checked',false);
		    		}else{
		    			jQuery(this).prop('checked',true);
		    		}
	    		})
	    		//如果全选中 将全选按钮改成选中状态
	    		var checkedlen = jQuery("input.check_item:checked").length;
    	        var inputlen = jQuery('.check_item').length;
	    		if(checkedlen == inputlen){
                    jQuery("#check_alls").prop('checked',true);  
                }else{
                    jQuery("#check_alls").prop('checked',false);  
                }
    		}
    	}

    	// 点击每一条数据的复选框
    	if(obj.className == 'check_item'){
            if(jQuery(obj).is(":checked")){
            	var checkedlen = jQuery("input.check_item:checked").length;
    	        var inputlen = jQuery('.check_item').length;
            	if(checkedlen == inputlen){
                    jQuery("#check_alls").prop('checked',true);  
                }else{
                    jQuery("#check_alls").prop('checked',false);  
                }
            }else{
                jQuery("#check_alls").prop('checked',false); 
            }
    	}
    }  
</script>
</html>

然后就是这个样子:

以上。

 

checkbox.css的代码:

input[type="checkbox" i] {
    -webkit-appearance: checkbox;
    box-sizing: border-box;
}
input, textarea, keygen, select, button {
    text-rendering: auto;
    color: initial;
    letter-spacing: normal;
    word-spacing: normal;
    text-transform: none;
    text-indent: 0px;
    text-shadow: none;
    display: inline-block;
    text-align: start;
    margin: 0em 0em 0em 0em;
    font: 13.3333px Arial;
}
input, textarea, keygen, select, button, meter, progress {
    -webkit-writing-mode: horizontal-tb;
}
.ui-radio, .ui-checkbox {
    position: relative;
    display: block;
    min-height: 20px;
    margin-top: 10px;
    margin-bottom: 10px;
}

.ui-selection{
  padding-right:20px;
}
  
.ui-selection-inline{
  float:left;
} 
.ui-radio-inline,.ui-checkbox-inline{
    display:inline-block;
    margin-right:30px;
    margin-top:0;
    margin-bottom:0;
}   
.ui-radio input[type="radio"] ,.ui-checkbox input[type="checkbox"] {
    opacity: 0;
    filter: progid:DXImageTransform.Microsoft.Alpha(Opacity=0);
    margin-left:4px;
}
.ui-radio label ,.ui-checkbox label{
    display: inline-block;
    position: relative;
    padding-left: 5px;
    font-size:12px;
    line-height:14px;
}
/*生成一个伪元素,作为美化版的单选按钮,先给伪元素添加一些样式*/
.ui-radio label::before{
    content: "";
    display: inline-block;
    position: absolute;
    width: 14px;
    height: 14px;
    left: 0;
    margin-top: -1px;
    margin-left: -21px;
    border: 1px solid #cccccc;
    border-radius: 50%;
    background-color: #fff;
    -webkit-transition: border 0.15s ease-in-out;
    -o-transition: border 0.15s ease-in-out;
    transition: border 0.15s ease-in-out;
}   
.ui-radio label::after {
    display: inline-block;
    position: absolute;
    content: " ";
    width: 8px;
    height: 8px;
    left: 3px;
    top: 3px;
    margin-left: -20px;
    border-radius: 50%;
    background-color: #448aff;
    -webkit-transform: scale(0, 0);
    -ms-transform: scale(0, 0);
    -o-transform: scale(0, 0);
    transform: scale(0, 0);
    -webkit-transition: -webkit-transform 0.1s cubic-bezier(0.8, -0.33, 0.2, 1.33);
    -moz-transition: -moz-transform 0.1s cubic-bezier(0.8, -0.33, 0.2, 1.33);
    -o-transition: -o-transform 0.1s cubic-bezier(0.8, -0.33, 0.2, 1.33);
    transition: transform 0.1s cubic-bezier(0.8, -0.33, 0.2, 1.33);
} 
/*给单选按钮的勾选状态添加不同的样式*/
.ui-radio input[type="radio"]:checked + label::after {
     -webkit-transform: scale(1, 1);
    -ms-transform: scale(1, 1);
    -o-transform: scale(1, 1);
    transform: scale(1, 1);
} 
.ui-checkbox label::before {
    content: "";
    display: inline-block;
    position: absolute;
    width: 15px;
    height: 14px;
    left: 0;
    margin-left: -20px;
    border: 1px solid #cccccc;
    border-radius: 3px;
    background-color: #fff;
    -webkit-transition: border 0.15s ease-in-out, color 0.15s ease-in-out;
    -o-transition: border 0.15s ease-in-out, color 0.15s ease-in-out;
    transition: border 0.15s ease-in-out, color 0.15s ease-in-out;
}   
.ui-checkbox input[type="checkbox"]:checked + label::before {
    background-color: #448aff;
    border-color: #448aff;
}
.ui-checkbox label::after {
    display: inline-block;
    position: absolute;
    width: 14px;
    height: 14px;
    left: -1px;
    top: 0px;
    margin-left: -20px;
    padding-left: 3px;
    padding-top: 1px;
    font-size: 12px;
    color: #fff;

}  
.ui-checkbox input[type="checkbox"]:checked + label::after {
     /*对勾*/
    content: '\00a0';
    display: inline-block;
    border: 3px solid #fff;
    border-right-width: 0;
    border-top-width: 0;
    width: 5px;
    height: 3px;
    -webkit-transform: rotate(-50deg);
    position: absolute;
    top: 3px;
    left: 3px;
}

a{
    text-decoration: none;
    color:#448aff;
}
.switchbutton{
  display: inline-block  ;
  position: relative;
  font-size: 12px;
  width: 42px;
  height: 20px ;
  line-height: 42px;
  }
.switchbutton input   
   { display: none }
  
.switchbutton input:checked + i:before {  
    border-color: #448aff ;
    -webkit-box-shadow: #448aff 0 0 0 16px inset  ;
    box-shadow: #448aff 0 0 0 16px inset  ;
    background-color: #448aff  ;
    transition: background-color 0.3s  ;
    -webkit-transition: background-color 0.3s;}
  
.switchbutton input:checked + i:after { 
    left: 22px }
      
.switchbutton i   {
    width: 42px ;
    height: 20px ;
    position: absolute  ;
    z-index: 2  ;
    border: 0  ;
    background: none  ;
    outline: 0;}
  
.switchbutton i:before  { 
    content: ''  ;
    width: 42px  ;
    height: 20px  ;
    background-color: #ccc  ;
    -webkit-border-radius: 10px;
    -moz-border-radius: 10px;
    border-radius: 10px;
    border-radius: 10px;
    cursor: pointer  ;
    display: inline-block  ;
    position: relative  ;
    vertical-align: top  ;
    -webkit-box-sizing: content-box  ;
    box-sizing: content-box  ;
    -webkit-box-shadow: #dfdfdf 0 0 0 0 inset ;
    box-shadow: #dfdfdf 0 0 0 0 inset ;
    -webkit-transition: border 0.3s, background-color 0.3s  ;
    transition: border 0.3s, background-color0.3s ;
    -webkit-background-clip: content-box  ;
    background-clip: content-box ;
    -webkit-appearance: none;
    user-select: none;
    outline: none;
  }
  
.switchbutton i:after  {
      content: ''  ;
      width: 16px;
      height: 16px  ;
      position: absolute;
      top: 2px;
      left: 2px ;
      border-radius: 100% ;
      background-color: #fff ;
      -webkit-box-shadow: 0 1px 3px rgba(0, 0, 0, 0.4) ;
      box-shadow: 0 1px 3px rgba(0, 0, 0, 0.4)  ;
      -webkit-transition: left .2s  ;
      transition: left .2s ;
  }

label {
  display: block;
  line-height: 1;
  /*margin-bottom:10px;*/
}
label input {
  margin-top: 10px;
  margin-right: 20px
}
.switch-t{
  display:block;
  margin-bottom:5px}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值