jquery全選反選操作,jquery實現全選和取消全選

<!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

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
投稿專員的頭像投稿專員
上一篇 2024-12-10 18:44
下一篇 2024-12-10 18:44

相關推薦

發表回復

登錄後才能評論