//兩個按鈕 <input class="btn-primary" type="button" value="全選" id="selectBtn" onclick="selectAll()"/><input class="btn-primary" type="button" value="反選" i ...
//兩個按鈕
<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()+",";
});