简体   繁体   English

从表中的输入获取价值

[英]Get value from input in a table

I have the following code: 我有以下代码:

function create_row() {
    var table = document.getElementById("main_table");
    var n = table.rows.length;
    var m = table.rows[0].cells.length;
    var row = table.insertRow(-1);
    var cell = row.insertCell(0);
    cell.innerHTML = n;
    var cell = row.insertCell(1);
    cell.innerHTML = "<input size=10>";
    for (i=2; i<m; i++) {
        var cell = row.insertCell(i);
        cell.innerHTML = "<input size=4>";
    } 
}

function print_values() {
    var table = document.getElementById("main_table");
    for (i=1; i<table.rows.length; i++) {
        console.log(table.rows[i].cells[1].innerHTML);
    }
}

However, when I print values I get <input size="10"> . 但是,当我打印值时,得到<input size="10">

The problem is that when I create a row, I add manually some value to a table (in a webpage) and I want to print those values in a log. 问题是,当我创建一行时,我将一些值手动添加到表中(在网页中),并且希望将这些值打印在日志中。 How can I access those values, but not html of the input? 如何访问这些值,但不能访问输入的html?

I probably can assign an id to each input form and then access value keyword, but thought maybe it could suffice using table cells. 我可能可以为每个输入表单分配一个id,然后访问value关键字,但我认为使用表单元格就足够了。

The input element is a child of the table cell. input元素是表单元格的子级。 And you have to use .value to get the value of an input, not .innerHTML . 而且您必须使用.value来获取输入的值,而不是.innerHTML

console.log(table.rows[i].cells[1].childNodes[0].value)

You want the insides of your input, so you'll have to select that input. 您需要输入的内容,因此必须选择该输入。

yourExistingSelector.querySelector("input").innerHTML

In your case: 在您的情况下:

table.rows[i].cells[1].querySelector("input").innerHTML

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

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