简体   繁体   English

无法将输入数据从 HTML 表格单元格保存到 JavaScript 变量中

[英]Unable to save input data from HTML table cells into JavaScript variables

I have tried to put input from a user into cells of a table, which was created using HTML and to save that data into a variable of JavaScript.我试图将用户的输入放入表的单元格中,该表是使用 HTML 创建的,并将该数据保存到 JavaScript 的变量中。

I was trying to get the data from rows cell of table to JavaScript variables one by one, but I'm getting undefined in place of data when alerting the data after summit.我试图从表格的行单元格中逐一获取 JavaScript 变量的数据,但是在峰会后提醒数据时,我得到了未定义的数据。

HTML code HTML代码

 <table border = "2" cellpadding = "6" cellspacing = "6" bordercolor = "white" bgcolor = "red" class="center" id="mytable" >
        <tr>
            <th style="background: white;">EDUCATION</th>
            <th style="background: white;">INSTITUTE</th>
            <th style="background: white;">PERCENTAGE</th>
        </tr>
        <tr>
            <td style="color: white;">10 th</td>
            <td id="10inst"><input type="text"></td>
            <td  id="10per"><input type="text"></td>
        </tr>
        <tr>
            <td style="color: white;" >12 th</td>
            <td  id="12inst"><input type="text"></td>
            <td  id="12per"><input type="text"></td>
        </tr>
        <tr>
            <td style="color: white;">Graduaction</td>
            <td  id="gradinst"><input type="text"></td>
            <td  id="gradper"><input type="text"></td>           
        </tr>
        <tr>
            <td style="color: white;">Masters</td>
            <td id="masterinst"><input type="text"></td>
            <td id="masterper"><input type="text"></td>           
        </tr>

    </table><br><br>

JavaScript code JavaScript代码

var inst10 = document.getElementById("mytable").value;
var inst12 = document.getElementById("12inst").value;
var masterinst = document.getElementById("masterinst").value;
var per10 = document.getElementById("10per").value;
var per12 = document.getElementById("12per").value;
var masterper = document.getElementById("masterper").value;
var gradinst=document.getElementById("gradinst").value;
var gradper=document.getElementById("gradper").value;

There might be 2 problems in your code:您的代码中可能存在 2 个问题:

  1. Make sure you run your JavaScript only after you enter text in the input elements.确保仅在输入元素中输入文本后运行 JavaScript。

  2. You have <input> element inside <td> element. <td>元素中有<input>元素。 It means that when you getElementById of the <td> - you are not yet referencing the '' element.这意味着当您 getElementById 的<td>时 - 您还没有引用 '' 元素。 And the .value of '' is really undefined. '' 的.value确实是未定义的。 So to fix that:所以要解决这个问题:

    instead of代替

var inst12 = document.getElementById("12inst").value;

do:做:

var inst12 = document.getElementById("12inst").childNodes[0].value;

Full working example: https://jsfiddle.net/1dpg5j3q/完整的工作示例: https://jsfiddle.net/1dpg5j3q/

You must be add "id" attribute in input elements, not td elements or table elements您必须在输入元素中添加“id”属性,而不是 td 元素或表格元素

Like this;像这样;

<input id="12inst" type="text">

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

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