简体   繁体   English

如何在 html 表中循环并更改 javascript 中每个单元格的值

[英]how to loop in html table and change the value of each cell in javascript

I have my input table which already filled by the user.我的输入表已经由用户填写。 now what I am trying to do is to loop on my table and change the value of each cell td using my JavaScript.现在我要做的是在我的表上循环并使用我的 JavaScript 更改每个单元格td的值。 some of my cells has input field and others only have text between td tags.我的一些单元格有输入字段,而其他单元格只有td标签之间的文本。 I loop on my table correctly and I change the values of cells that has input field using the expression table.rows[R].cells[C].children[0].value = value;我在表格上正确循环,并使用表达式table.rows[R].cells[C].children[0].value = value; but I do not know how to change the cell that has only text between td tags without input field!但我不知道如何更改没有输入字段的 td 标签之间只有文本的单元格! tried to write table.rows[R].cells[C].value = value but its not working: my table code:试图写table.rows[R].cells[C].value = value但它不起作用:我的表格代码:

var table = document.getElementById('table');
var rowCount = table.rows.length;
var colCount = document.getElementById('table').rows[0].cells.length;
for (var r = 1; r < rowCount; r++) {
    for (var c = 1; c < colCount - 1; c++) {
        table.rows[r].cells[c].children[0].value = somevalue; // this works with cells that has input field but not with cells that has not 
        table.rows[r].cells[c] value = somevalue; // tried this line but not working
    }
}

Based on your explanation I understand that you want to modify the values of all td tags (input child or text child).根据您的解释,我了解您要修改所有 td 标记(输入子项或文本子项)的值。 In your snippet there are two problems in order to achieve this:在您的代码段中,要实现这一目标有两个问题:

  1. The start index of the “column loop” (c) is 1 and you loop until this index is less than the length of all columns. “列循环” (c) 的起始索引为 1,然后循环直到该索引小于所有列的长度。 This means that you'll loop through all the columns except the first and last one.这意味着您将遍历除第一列和最后一列之外的所有列。 Maybe this is your intention.也许这就是你的意图。
  2. You have a syntax error in the last line of the last for loop.最后一个 for 循环的最后一行有语法错误。

For your “main” problem, it seems like you are treating the cells the same even though some contain input elements and some text nodes.对于您的“主要”问题,即使某些单元格包含输入元素和某些文本节点,您似乎也将单元格视为相同。 You can simply solve this by adding some logic and depending on if the cell has children (an input field) or not (text-node) you treat it respectively.您可以通过添加一些逻辑来简单地解决这个问题,并根据单元格是否有子级(输入字段)或没有(文本节点)来分别处理它。 For text nodes we simply modify the innerHTML.对于文本节点,我们只需修改 innerHTML。

Here's a snippet which should do the job for you if I understood you correctly:如果我理解正确,这是一个应该为您完成工作的片段:

var table = document.getElementById("table");
var rowCount = table.rows.length;
var colCount = document.getElementById("table").rows[0].cells.length;

var somevalue = "modified value";

for (var r = 1; r < rowCount; r++) {
  
  for (var c = 0; c < colCount; c++) {
    const cell = table.rows[r].cells[c];

    if (cell.children.length) {
      cell.children[0].value = somevalue;
    } else {
      cell.innerHTML = somevalue;
    }

  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM