如何在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 code
<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>
答案 0 :(得分:1)
您的复选框name
为users
,id
为'deacTraining ...'。变化:
document.getElementsByName('deacTraining');
为:
document.getElementsByName('users');