我有一个HTML表格,每行一个单元格,包含五个复选框:
<td class="days">
<label><input type="checkbox" name="mon" id="mon" value="1" class="form-control" />M</label>
<label><input type="checkbox" name="tue" id="tue" value="2" class="form-control" />Tu</label>
<label><input type="checkbox" name="wed" id="wed" value="3" class="form-control" />W</label>
<label><input type="checkbox" name="thu" id="thu" value="4" class="form-control" />Th</label>
<label><input type="checkbox" name="fri" id="fri" value="5" class="form-control" />F</label>
</td>
我需要我的javaScript来检查每个复选框的值以与其他东西进行比较。我已经尝试了以下方法来获取复选框mon,tue,wed,thu和fri的值,但它们似乎不起作用:
var table = document.getElementById("leaveTable");
var monValue = table.rows[row].cells[5].namedItem("mon").firstChild.value;
(包含复选框的单元格的索引为5)
我有多行,除了行索引之外都是相同的,所以我可以将行索引作为参数传递给我的函数,并且无论行如何,相同的函数都可以工作。
我只使用javaScript而不是JQuery,所以请只回答javaScript建议。感谢
答案 0 :(得分:0)
您可以在该行上使用querySelectorAll()来获取其中的所有输入元素
var idx = 0;
var table = document.getElementById("leaveTable");
var inputs = table.rows[idx].querySelectorAll('input[type="checkbox"]'); //'input[type="checkbox"]:checked' if you want only checked
for (var i = 0; i < inputs.length; i++) {
snippet.log(inputs[i].value + ':' + inputs[i].name + ':' + inputs[i].nextSibling.nodeValue)
}
&#13;
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
<table id="leaveTable">
<tr>
<td class="days">
<label>
<input type="checkbox" name="mon" id="mon" value="1" class="form-control" />M</label>
<label>
<input type="checkbox" name="tue" id="tue" value="2" class="form-control" />Tu</label>
<label>
<input type="checkbox" name="wed" id="wed" value="3" class="form-control" />W</label>
<label>
<input type="checkbox" name="thu" id="thu" value="4" class="form-control" />Th</label>
<label>
<input type="checkbox" name="fri" id="fri" value="5" class="form-control" />F</label>
</td>
</tr>
</table>
&#13;