我需要从不是唯一的矩阵单元中获取值。 该模型是原始模型的简化,原始模型是一个巨大的矩阵, 这就是为什么我对此着迷的原因: document.getElementById(“ myTable”)。rows [0] .cells [0]。 XXXXXX 。
对象:应该在该指令中使用哪种属性或方法来获取复选框的值(是/否)或(1,0)。
谢谢!!!!
<!DOCTYPE html>
<html>
<head>
<style>
table,
td {
border: 1px solid black;
}
</style>
</head>
<body>
<p>Tratando de obtener valores de los checkbox dentro de una tabla.</p>
<table id="myTable">
<tr>
<td id='C1'> <input type='checkbox' checked='checked'> </td>
<td id='C2'> <input type='checkbox' checked='checked'> </td>
</tr>
<tr>
<td id='C3'> <input type='checkbox' checked='checked'> </td>
<td id='C4'> <input type='checkbox' checked='checked'> </td>
</tr>
</table>
<p id="print1"></p>
<p id="print2"></p>
<p id="print3"></p>
<p id="print4"></p>
<br>
<button onclick="myFunction()">Intentarlo</button>
<script>
function myFunction() {
document.getElementById("print1").innerHTML = document.getElementById("myTable").rows[0].cells[0];
}
</script>
</body>
</html>
答案 0 :(得分:0)
复选框的选中/未选中状态存储在其checked
属性中。请注意,您获得的cell
是td
,而不是input
-您需要获取td
的(单元格)子元素,它是{{ 1}}(出于安全考虑,我使用的是input
;如果querySelector
始终是第n个孩子,则可以使用input
(其中children[n]
是从0开始的) )。见下文:
n
function myFunction() {
document.getElementById("print1").innerHTML = document.getElementById("myTable").rows[0].cells[0].querySelector('input[type="checkbox"]').checked;
}