导读:今天讲解的是jquery怎么实现复选框checkbox全选,取消全选,这样的功能的模块网站后台是必不可少的,例如文章的批量删除,批量移动等。jsp
今天讲解的是jquery怎么实现复选框checkbox全选,取消全选,这样的功能的模块网站后台是必不可少的,例如文章的批量删除,批量移动等。
jsp中checkbox复选框的个数是依据从数据库中取出值的条数决定的,是Iterator循环遍历出来的。
<td class="rd8">
<input type="checkbox" name="selectFlag" id="selectFlag" value="<%=user.getUser_id()%>">
</td>
第一种方法:
//全选
$("#checkAll").click(function() {
if (this.checked) {
$("input[name='selectFlag']:checkbox").each(function() { //遍历所有的name为selectFlag的 checkbox
$(this).attr("checked", true);
})
}
})
//取消全选
$("#delCheckAll").click(function() {
if (this.checked) {
$("input[name='selectFlag']:checkbox").each(function() { //遍历所有的name为selectFlag的 checkbox
$(this).attr("checked", false);
})
}
})
更简单直接的一种:
$("#checkAll").click(function() {
if (this.checked) {
$("input[name='selectFlag']:checkbox").each(function() { //遍历所有的name为selectFlag的 checkbox
$(this).attr("checked", true);
})
} else { //反之 取消全选
$("input[name='selectFlag']:checkbox").each(function() { //遍历所有的name为selectFlag的 checkbox
$(this).attr("checked", false);
//alert("f");
})
}
})
这样就搞定了,是不是很简单呢!