获取checkbox勾选的id

需求描述:做批量删除或者批量修改的时候需要获得多个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);
   }
}

总结:这两种都可以嗷

原文地址:https://www.cnblogs.com/xuchao0506/p/9766740.html