简体   繁体   English

使用javascript遍历html表单元

[英]loop through the html table cells using javascript

I am trying hard to loop through the table cells and insert the values inside them.But i don't know what is wrong with my code.Please help me out.I know there is some silly mistake.Please let me know what it is. 我正在努力遍历表单元格并将值插入其中。但是我不知道我的代码有什么问题。请帮帮我。我知道这里有一些愚蠢的错误。请让我知道它是什么。 Here is my code- 这是我的代码-

 var data = response.datas.length;
              var table = document.getElementById('itemsval');
              for(var i=0; i<data; i++ ){
                var row = table.insertRow(i);
                for(var j=0; j<7; j++){
                 var cell = row.insertCell(j);


                cell[i][j].innerHTML = response.datas[i].p_name;
                 cell[i][j].innerHTML = response.datas[i].p_details;
                 cell[i][j].innerHTML = response.datas[i].p_qty;
                 cell[i][j].innerHTML = response.datas[i].p_rate;
                 cell[i][j].innerHTML = response.datas[i].p_dis;
                 cell[i][j].innerHTML = response.datas[i].p_tax;
                 cell[i][j].innerHTML = response.datas[i].p_tot; 
                }

              }

I keep getting the error- Cannot read property '0' of undefined 我不断收到错误-无法读取未定义的属性“ 0”

Thanks in advance. 提前致谢。

On the first iteration, this line 在第一次迭代中,此行

cell[i][j].innerHTML = response.datas[i].p_name;

tries to access a property called 0 within cell . 尝试访问cell名为0的属性。 cell won't have a property called 0 (from the variable i ). cell将没有名为0的属性(来自变量i )。 If it did, that property's value wouldn't have a property with the name 0 (from the variable j ) either. 如果确实如此,则该属性的值也不会具有名称0 (来自变量j )的属性。

Separately, repeated writing to the same place: 分别重复写入同一位置:

cell[i][j].innerHTML = response.datas[i].p_name;
cell[i][j].innerHTML = response.datas[i].p_details;

...will cause later values to overwrite earlier values. ...将导致以后的值覆盖以前的值。

Instead, you want to add a cell to the row as you go, and set that cell's content. 相反,您想在行中添加一个单元格,并设置该单元格的内容。 Let's give ourselves a handy function to do that: 让我们给自己一个方便的功能来做到这一点:

function addCell(row, content) {
    var cell = row.insertCell();
    cell.appendChild(
        document.createTextNode(content)
    );
    return cell;
}

That adds the cell, sets its content in a way where you don't have to worry about HTML markup, and returns the cell. 这样就添加了单元格,以不必担心HTML标记的方式设置其内容,然后返回了该单元格。

Now, let's use it in your code: 现在,让我们在代码中使用它:

// Get the table
var table = document.getElementById('itemsval');

// Add the rows
response.datas.forEach(function(entry) {
    var row = table.insertRow();
    addCell(row, entry.p_name);
    addCell(row, entry.p_details);
    addCell(row, entry.p_qty);
    addCell(row, entry.p_rate);
    addCell(row, entry.p_dis);
    addCell(row, entry.p_tax);
    addCell(row, entry.p_tot); 
});

Note how that code loops through the response.datas array via Array#forEach . 注意该代码如何通过Array#forEach遍历response.datas数组。 You have lots of other options (that's also an answer of mine) , but it seems the simplest in this case. 您还有很多其他选择 (这也是我的答案) ,但是在这种情况下,这似乎是最简单的。

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

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