input checkbox 全选和反选

全选和反选(仅供参考) 

注:请忽略我的排版O(∩_∩)O哈哈~

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>全选/反选</title>
		<script type="text/javascript" src="js/jquery-3.3.1.js" ></script>
		<style type="text/css">
			html,body{ height: 100%; padding: 0; margin: 0; }
			#div{ width: 100%;height: 100%; }
			.xh{ width: 10%; float: left; }
			.sj{ width: 50%;float: left; }
			.tealgin{ text-align: center; }
		</style>
	</head>
	<body>
		<div id="div">
			<div>
				<div class="xh">
					<input type="checkbox" id="checkId" onclick="check(this.id,'uncheckId')"/>全选/ 
					<input type="checkbox" id="uncheckId" onclick="uncheck(this.id,'checkId')"/>反选
				</div>
				<div class="sj">标题</div>
			</div>
			
			<div style="clear: both;">
				<div class="xh tealgin">
					<input type="checkbox" name="ids" value="1"/>
				</div>
				<div class="sj">1</div>
			</div>
			<div style="clear: both;">
				<div class="xh tealgin">
					<input type="checkbox" name="ids" value="2"/>
				</div>
				<div class="sj">2</div>
			</div>
			<div style="clear: both;">
				<div class="xh tealgin">
					<input type="checkbox" name="ids" value="3"/>
				</div>
				<div class="sj">3</div>
			</div>
			<div style="clear: both;">
				<div class="xh tealgin">
					<input type="checkbox" name="ids" value="4"/>
				</div>
				<div class="sj">4</div>
			</div>
			<div style="clear: both;">
				<div class="xh tealgin">
					<input type="checkbox" name="ids" value="5"/>
				</div>
				<div class="sj">5</div>
			</div>
			<div style="clear: both;">
				<div class="xh tealgin">
					<input type="checkbox" name="ids" value="6"/>
				</div>
				<div class="sj">6</div>
			</div>
			<div style="clear: both;">
				<div class="xh tealgin">
					<input type="checkbox" name="ids" value="7"/>
				</div>
				<div class="sj">7</div>
			</div>
		</div>
	<script type="text/javascript">
		function check(id,uncheck){
			var checkboxId = document.getElementById(id);
			var uncheckboxId = document.getElementById(uncheck);
			uncheckboxId.checked = false;
			var selectList = $("input[name='ids']");
			if(checkboxId.checked==true){
				for(var i = 0;i<selectList.length;i++){
				  if(selectList[i].type=="checkbox" ){
                    	selectList[i].checked= true;
                    }
				}
			}else{
				for(var i=0 ; i < selectList.length; i++){
                    if(selectList[i].type=="checkbox" ){
                    	selectList[i].checked= false;
                    }
               }
			}
		}
		
		function uncheck(id,check){
			var uncheckboxId = document.getElementById(id);
			var checkboxId = document.getElementById(check);
			checkboxId.checked = false;
			var unSelectList = $("input[name='ids']");
			for (var i = 0;i < unSelectList.length;i++) {
				if(unSelectList[i].checked == false){
					unSelectList[i].checked = true;
				}else{
					unSelectList[i].checked = false;
				}
			}
		}
		
		$(function(){
			$("input[name='ids']").change(function(){
				var total = $("input[name='ids']");console.info(total.length)
				var checkboxId = document.getElementById("checkId");
				var checkedNum = $("input[name='ids']:checked");console.info(checkedNum.length)
				if(total.length > checkedNum.length){
					if(checkboxId.checked==true){
						checkboxId.checked = false;
					}
				}else{
					if(checkboxId.checked==false){
						checkboxId.checked = true;
					}
				}
			});
		});
	</script>
	</body>
</html>

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值