您的位置:首页 > Web前端 > JQuery

HTML页面checkbox的全选反选全不选-基于jquery

2020-01-14 05:31 302 查看

直接上代码:

<!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>
  • 点赞
  • 收藏
  • 分享
  • 文章举报
ChyoD1811 发布了15 篇原创文章 · 获赞 3 · 访问量 1624 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: