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>

(0)
小多多的头像小多多创始人

相关推荐

发表回复

登录后才能评论