使用jQuery做复选框的全选与取消

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<script src="js/jquery.min.js" type="text/javascript" charset="utf-8"></script>
	</head>
	<body>
		<input type="checkbox"  id="qx"  />全选<br>
		<input type="checkbox" class="abc" /><br />
		<input type="checkbox" class="abc" /><br />
		<input type="checkbox" class="abc" /><br />
	</body>
	<script type="text/javascript">
		$("#qx").click(function(){
            //得到id="qx"的标签的checked属性的值
			var x= $("#qx").prop('checked');
            //将class属性为.abc的标签的checked属性设置成x
			$(".abc").prop('checked',x);	
		})
	</script>
</html>

其中的下边这句代码可以做更换:

$(".abc").prop('checked',x);	

 上边这行代码可以更换为:

//循环遍历class属性为.abc的标签
$(".abc").each(function(){
	//this表示每次遍历到的标签
	$(this).prop('checked',x);
})	

猜你喜欢

转载自blog.csdn.net/My_name_is_ZwZ/article/details/83479748
今日推荐