简单的全选和反选功能以及选中传值实现
- 作者: 特别靠谱先生
- 来源: 51数据库
- 2021-08-23
//两个按钮
<input class="btn-primary" type="button" value="全选" id="selectbtn" onclick="selectall()"/>
<input class="btn-primary" type="button" value="反选" id="selectbtn1" onclick="reverse()"/>
//全选和反选方法
function selectall(){
$("#contenttable:checkbox").attr("checked",true);
$("#selectbtn").attr("onclick","reverse()");
}
function reverse(){
$("#contenttable:checkbox").each(function(){
$(this).attr("checked",!$(this).attr("checked"));
});
$("#selectbtn").attr("onclick","selectall()");
}
//表单
<table id="contenttable" >
<tr>
<th></th><th>序号</th><th>...</th>
</tr>
<c:foreach items="${list}" var="list" varstatus="index">
<tr>
<td><input name="items" type="checkbox" value="${list.id}"/></td>
<td>${index.index+1}</td>
<td>...</td>
</tr>
</c:foreach>
</table>
//需要传入后台的id值
var ids="";
$("[name=items]:checkbox:checked").each(function(){
ids+=$(this).val()+",";
});