Directly on the code:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="./jquery-3.3.1.min.js" ></script>
</head>
<body>
<script>
$(function() {
$("#selectAll").click(function() {
$(":checkbox[name='ids']").prop("checked", this.checked); // this指代的你当前选择的这个元素的JS对象
});
});
$(function() {
$("#selectRef").click(function() {
$("#selectAll").prop("checked", false);
$(":checkbox[name='ids']").each(function ()
{
$(this).prop("checked",!this["checked"]);
});
});
});
</script>
</head>
<body>
<table id="tab1" border="1" width="80%" align="center">
<thead>
<tr>
<th><input type="checkbox" id="selectAll" />全选全不选<input type="button" id="selectRef" value="反选"/></th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="checkbox" name="ids" /></td>
</tr>
<tr>
<td><input type="checkbox" name="ids" /></td>
</tr>
<tr>
<td><input type="checkbox" name="ids" /></td>
</tr>
<tr>
<td><input type="checkbox" name="ids" /></td>
</tr>
<tr>
<td><input type="checkbox" name="ids" /></td>
</tr>
</tbody>
</table>
</body>
</body>
</html>