Jquery实现 全选反选
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../jquery-3.5.0.min.js"></script>
</head>
<body>
<input type="checkbox" id="a1">全选 <input type="button" value="反选" id="a3"><br />
<input type="checkbox" class="a2"> 张三 <br />
<input type="checkbox" class="a2"> 李四<br />
<input type="checkbox" class="a2"> 王五
<script>
$('#a1').click(function (){
let flag = $(this).prop('checked');
$('#a1,.a2').prop('checked', flag);
});
$('.a2').click(function (){
xiao();
});
$('#a3').click(function () {
let xz = $('.a2');
xz.each(function (elm){
$(this).prop('checked',!$(this).prop('checked'));
xiao();
})
});
function xiao(){
let sy = $('.a2').length;
let xz= $('.a2:checked').length;
$('#a1').prop('checked', sy== xz);
};
</script>
</body>
</html>