Javascript访问childNode
我有以下问题,但我还没有找到解决问题的方法。我使用了一个动态生成的表格,该表格由4列组成:复选框,文本值和隐藏输入。
I have the following problem and I have not found a way to solve it. I'm using a dynamically generated table, which consists of 4 columns: a checkbox, text values and a hidden input.
echo "<tr><td><div class='input-containerh'><input type='checkbox'></div></td>";
echo "<td>" .$s. "</td>";
echo "<td>" .$L. and some vars............ "</td>";
echo "<td><input type='hidden' value=" . some vars.... "></td>";
echo "</tr>";
由于我想格式化复选框,因为在某些浏览器中,单元格让我太大了,一个DIV格式输入(复选框),它适用于我。
As I want to format the checkbox, because in some browsers the cell makes me too big, I use a DIV to format input (checkbox) and it works good for me.
现在问题是一个JavaScript函数,检测输入是否选择,现在不工作:
Now the problem is a javascript function that detect if the input was select and now does not work me:
function deleteRow(tableID) {
try {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
for(var i=1; i<rowCount; i++) {
var row = table.rows[i];
var chkbox = row.cells[0].childNodes[0];
if(null != chkbox && true == chkbox.checked) {
table.deleteRow(i);
rowCount--;
i--;
}
使用萤火虫我发现问题是:
Using firebug I found that the problem was:
var chkbox = row.cells[0].childNodes[0];
因为它指的是DIV而不是输入。
because it refers to "DIV" not at "input".
我如何才能到达DIV内部和单元格内的input元素?
How i can reach the "input" element that is within "DIV" and also inside the cell?
感谢您的帮助。 >
Thanks for help.
由于 var chkbox = row.cells [0] .childNodes [0]
使用此HTML:
Since the var chkbox = row.cells[0].childNodes[0]
worked with this HTML:
<tr><td><input> type='checkbox'></td>
更改HTML如下:
changing the HTML as follows:
<tr><td><div class='input-containerh'><input> type='checkbox'></div></td>
表示 row.cells [0] .childNodes [0] code>现在将指向
div
元素。要在 div
中获取元素$ c>元素,您只需使用
childNodes访问下一级别的子元素
。因此,简而言之,您可以通过以下方式访问 input
:
means that row.cells[0].childNodes[0]
will now point to the div
element. To get the input
element inside div
, you just access the next level of children with childNodes
. So, in a nutshell, you can get access to your input
with this:
var chkbox = row.cells[0].childNodes[0].childNodes[0];