我如何在JS中获取和循环deacTraining值?
在此给定代码中,获得deacTraining的值后,复选框的长度为0。
html代码段
<c:forEach var="acBatch" items="${batchBeanAll}" varStatus="loop">
<c:if test="${acBatch.getBatchStatus() == 'Active'}">
<c:set var="counter" value="${counter + 1}" scope="page"/>
<tr>
<td scope='col'><input id="deacTraining${loop.index }" type='checkbox' name='users'value='${acBatch.getBatchCode()}'/></td>
<td scope='col'>${acBatch.getBatchDescription()}</td>
<td scope='col'>${acBatch.getBatchSize()}</td>
<td scope='col'>${acBatch.getBatchStatus()}</td>
</tr>
</c:if>
<input value="Continue" type="submit" onclick="return checkDeactivateTraining();"/>
js代码
<script>
function checkDeactivateTraining(){
var checkboxes = document.getElementsByName('deacTraining');
for (var i=0; i<checkboxes.length; i++){
if (checkboxes[i].checked)
return true;
}
alert("Please select a training to deactivate.");
return false;
}
</script>
最佳答案
复选框的name
是users
,id
是'deacTraining ...'。更改:
document.getElementsByName('deacTraining');
至:
document.getElementsByName('users');