需求描述:做批量刪除或者批量修改的時候需要獲得多個id,根據checkbox勾選來獲取對應的d
兩種方法:
//html代碼
<table id="table1"> <tr th:each="pac : ${list}" th:id="${pac.packajeId}">
<td>
<label class="mt-checkbox mt-checkbox-single mt-checkbox-outline">
<input type="checkbox" class="checkboxes" th:value="${pac.packajeId}" name="selectedIds" /> <span></span>
</label>
</td>
<td th:text="${pac.packajeName}"></td>
<td th:text="${pac.packajeCode}"></td> </tr>
</table>
//js代碼 方法一
var checkVal = [];
$("input.checkboxes[name='selectedIds']:checkbox").each(function() {
if ($(this).is(":checked")) {
var s= $(this).val();
checkVal.push(s);
ableMany=true;
}
});
//js代碼 方法二 var obj = document.getElementsByName("selectIds");
var checkVal= [];//獲取選中的id
for(var k in obj){
if(obj[k].checked){
checkVal.push(obj[k].value);
}
}
總結:這兩種都可以嗷