jquery实现全选、全不选、反选操作

时间:2022-07-12 13:15:43
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>新建网页</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="description" content=""/>
<meta name="keywords" content=""/>
<script type="text/javascript" src="../jquery-1.4.4.js" ></script> <script type="text/javascript">
function selAll(){
$(".ho").attr('checked',true);
}
function selAllNot(){
$(".ho").attr('checked',false);
}
function selFan(){
//反选操作
//获得全部复选框、遍历、判断当前选中情况
for(var i=0; i<$(".ho").length; i++){
var flag = $(".ho:eq("+i+")").attr("checked");
$(".ho:eq("+i+")").attr("checked",!flag);
}
}
</script>
</head>
<body>
<h2>复选框操作</h2> <input type="checkbox" name="hobby[]" id="hobbya" class="ho" value='a'>篮球
<input type="checkbox" name="hobby[]" id="hobbyb" class="ho" value='b'>足球
<input type="checkbox" name="hobby[]" id="hobbyc" class="ho" value='c'>登山
<input type="checkbox" name="hobby[]" id="hobbyd" class="ho" value='d'>排球 <br /><br />
<input type="button" value="全选" onclick="selAll()" />
<input type="button" value="全不选" onclick="selAllNot()" />
<input type="button" value="反选" onclick="selFan()" />
</body>
</body>
</html>