简体   繁体   English

document.getElementById(...)…不能正确地从表中获取文本

[英]document.getElementById(…)… not properly grabbing my text from the table

HTML/JavaScript n00b here. HTML / JavaScript n00b。

I have a table whose bottom row is composed of input cells: 我有一个表,其底行由输入单元格组成:

<table class="convtbl" id="table1">

   <tr>
      <td>Distance 1 (in miles)</td>
      <td>Time 1 (hh:mm:ss)</td>
      <td>Distance 2 (in miles)</td>
      <td>Time 2 (hh:mm:ss)</td> 
   </tr>

   <tr>
      <td><input type="text"/></td>
      <td><input type="text"/></td>
      <td><input type="text"/></td>
      <td><input type="text"/></td>
   </tr>

</table>

I'm trying to make a function that changes the values inside the input cell. 我正在尝试制作一个函数来更改输入单元格内的值。 I haven't fully made the function, but as a test I used an alert to see if the text was properly grabbed out of column 1. Apparently it wasn't, because the alert is undefined . 我还没有完全实现该功能,但是作为测试,我使用了一个警报来查看文本是否从第1列中正确地抓取。显然不是,因为警报undefined Any idea why? 知道为什么吗?

function tdconvert()
{
    var d1 = document.getElementById("table1").rows.item(1).cells.item(0).firstChild.textConent;
    var t1 = document.getElementById("table1").rows.item(1).cells.item(1).firstChild.textConent;
    var d2 = document.getElementById("table1").rows.item(1).cells.item(2).firstChild.textConent;
    var t2 = document.getElementById("table1").rows.item(1).cells.item(3).firstChild.textConent;
    alert(d1);
}

这些是输入,您可能是说element.value ,而不是textConent ,如果这就是您要获取的内容,则至少应由textContent表示。

var d1 = document.getElementById("table1").rows.item(1).cells.item(0).firstChild.value;

you are accessing textContent attribute of textbox, thats the reason its throwing undefined. 您正在访问文本框的textContent属性,这就是其未定义抛出的原因。 Try value attribute of textbox 尝试文本框的value属性

document.getElementById("table1").rows.item(1).cells.item(0).firstChild.value; document.getElementById(“ table1”)。rows.item(1).cells.item(0).firstChild.value;

It works i tested 我测试过了

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

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