达永编程网

程序员技术分享与交流平台

jq实现全选,全取消,反选(jq全选反选取消代码)

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

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言