1. 程式人生 > >利用jQuery實現全選、全不選、反選(button)

利用jQuery實現全選、全不選、反選(button)

htm nag ttr check n) 足球 text ctype cti

<!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;
});
});
</script>
</html>
技術分享圖片

利用jQuery實現全選、全不選、反選(button)