<!doctype html>
<html>
<head>
<meta charset=”UTF-8″>
<title>Document</title>
<script type=”text/javascript” src=”jquery.min.js”></script>
</head>
<body>
<input type=”checkbox” id=”checkbox1″><label for=”checkbox1″>庫里</label><br>
<input type=”checkbox” id=”checkbox2″><label for=”checkbox2″>科比</label><br>
<input type=”checkbox” id=”checkbox3″><label for=”checkbox3″>麥迪</label><br>
<input type=”checkbox” id=”checkbox4″><label for=”checkbox4″>鄧肯</label><br>
<input type=”checkbox” id=”checkbox5″><label for=”checkbox5″>奧尼爾</label><br><br>
<button>全選</button><button>全不選</button><button>反選</button>
</body>
</html>
<script type=”text/javascript”>
$(function(){
//匹配第一個button
$(‘:button:eq(0)’).click(function(){
//全部選中 checked=true,在前台就是表示選中
$(‘:checkbox’).attr(‘checked’,true);
});
//匹配第二個button
$(‘:button:eq(1)’).click(function(){
//全部取消 checked=false,在前台就是表示未選中
$(‘:checkbox’).attr(‘checked’,false);
});
//匹配第三個button
$(‘:button:eq(2)’).click(function(){
//查找每一個複選框,然後取相反
$(‘:checkbox’).each(function(){
$(this).attr(‘checked’,!$(this).attr(‘checked’));
});
});
})
</script>
原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/230929.html