简体   繁体   English

使用 Javascript 从 HTML 表格输入单元格中获取值

[英]Get value from HTML table input cell with Javascript

I have created a HTML table dynamically with Javascript where the first column consists of text fields, the second column consists of input fields and the third column consists of text fields, which works fine:我用 Javascript 动态创建了一个 HTML 表,其中第一列由文本字段组成,第二列由输入字段组成,第三列由文本字段组成,工作正常:

nrOfRows = document.getElementById("myId").value; //get nrOfRows from input
var i;
var row;
var cell1;
var cell2;
var cell3;

for (i = 0; i < nrOfRows; i++) {

    row = table.insertRow(i); //table is defined earlier
    cell1 = row.insertCell(0);
    cell2 = row.insertCell(1);
    cell3 = row.insertCell(2);
    cell1.innerHTML = "some text"; //Put "some text" in all column1 fields
    cell2.innerHTML = '<input type="text" class="form-control" size="5" maxlength="4" />'; //Make all fields in column2 input fields.

}

After this I type something in the input fields (column2) and try to take this input and put it in column3, by doing this:在此之后,我在输入字段(第 2 列)中键入一些内容并尝试将此输入放入第 3 列,方法如下:

for (var r = 0; r < nrOfRows; r++) {    
        table.rows[r].cells[2].innerHTML = table.rows[r].cells[1].innerHTML;
    }

This does not work.这不起作用。 Instead of putting the values from the input fields in the third column it replaces the text fields in the third columns with new input fields , like it is taking the HTML code instead of the values.它不是将输入字段中的放在第三列中,而是将第三列中的文本字段替换为新的输入字段,就像使用 HTML 代码而不是值一样。

This works (taking the values from column1 into column3):这有效(将 column1 中的值放入 column3):

  for (var r = 0; r < nrOfRows; r++) {  
        table.rows[r].cells[2].innerHTML = table.rows[r].cells[0].innerHTML;
    }

Any clues on how I can get the values from the input fields instead of the HTML code for it?关于如何从输入字段而不是 HTML 代码中获取值的任何线索?

This is what I get.这就是我得到的。 I want the numbers from the middle column to go into the right column, but instead it replaces everything with new input fields:我希望中间列的数字进入右列,但它用新的输入字段替换所有内容:

截屏

You're directly accessing cells[1] and getting its innerHTML whereas cells[1] has a children and you need to access it value instead of innerHTML like children[0].value .您直接访问cells[1]并获取其innerHTML,而cells[1]有一个子项,您需要访问它的value而不是像children[0].value那样的innerHTML

Check accessing DOM Element Objects here在此处检查访问DOM 元素对象

Also, you can use querySelector .此外,您可以使用querySelector

 var table; window.onload = function() { table = document.getElementById("test"); nrOfRows = 3;//document.getElementById("myId").value; //get nrOfRows from input var i; var row; var cell1; var cell2; var cell3; for (i = 0; i < nrOfRows; i++) { row = table.insertRow(i); //table is defined earlier cell1 = row.insertCell(0); cell2 = row.insertCell(1); cell3 = row.insertCell(2); cell1.innerHTML = "some text"; //Put "some text" in all column1 fields cell2.innerHTML = '<input type="text" class="form-control" size="5" maxlength="4" />'; //Make all fields in column2 input fields. } } function GetValue() { for (var r = 0; r < nrOfRows; r++) { table.rows[r].cells[2].innerHTML = table.rows[r].cells[1].children[0].value; } }
 <table id="test" border="1"> </table> <input type="button" value="Get Value" onclick="GetValue()" />

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

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