继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

使用jQuery实现全选、全不选、 反选(button)

安卓入门学习视频
关注TA
已关注
手记 267
粉丝 68
获赞 387

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="jquery-1.8.3.js"></script>
</head>
<body>
<button>全选</button>
<button>全不选</button>
<button>反选</button>
<hr>
<div id="main">
<input type="checkBox">篮球
<br>
<input type="checkBox">羽毛球
<br>
<input type="checkBox">乒乓球
<br>
<input type="checkBox">足球
<br>
<input type="checkBox">橄榄球
<br>
<input type="checkBox">棒球
</div>
</body>
<script type="text/javascript">
// 第一种方法
// 全选
/$('button').eq(0).click(function(){
$('input').attr('checked',true);
});
// 全不选
$('button').eq(1).click(function(){
$('input').attr('checked',false);
});
// 反选
$('button').eq(2).click(function(){
$('input').each(function(){
this.checked=!this.checked;
});
});
/

// 第二种方法:
// 全选
$('button:eq(0)').click(function(){
$(':checkBox').attr({'checked':true});
});
// 全不选
$('button:eq(1)').click(function(){
// 表单选择器
$(':checkBox').attr({'checked':false});
});
// 反选
$('button:eq(2)').click(function(){
// 遍历每一个checkBox
$(':checkBox').each(function(){
$(this).attr({'checked':!$(this).attr('checked')});
});
});
</script>
</html>

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP