有你在真好 的个人博客
jq实现全选,全取消,反选
阅读:2313 添加日期:2021/3/27 23:28:19 原文链接:https://www.toutiao.com/item/6326820556737872386/

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

ICP备案号:苏ICP备14035786号-1 苏公网安备 32050502001014号