简体   繁体   English

用Javascript访问表格单元格值

[英]Table cell value accessing in Javascript

I have an html table. 我有一个html表。 Where each table row contains several input text areas and each input text area has unique ids assigned by javascript dynamically.Now I want to put all that ids of the 1st column of my table into an array. 每个表格行包含几个输入文本区域,每个输入文本区域具有由javascript动态分配的唯一ID。现在,我想将表格第一列的所有ID放入一个数组中。 I am facing problem in this matter. 我在这件事上面临问题。 Help me please. 请帮帮我。

Html code of my table is as follows: 我表的HTML代码如下:

<table id="POITable">
    <tr>
        <th>Item</th>
        <th>Brand</th>
        <th>UOM</th>
        <th>Quantity</th>
        <th>Remarks</th>
    </tr>

    <tr style="visibility:hidden;">             <!-- This is just a dummy row-->
        <td><input size=25 type="text"/></td>
        <td><input size=25 type="text"/></td>
        <td><input size=25 type="text"/></td>
        <td><input size=25 type="text"/></td>
        <td><input size=25 type="text"/></td>
        <td><img alt="Icon" src="/assets/add-icon.png" id="addmorePOIbutton" onclick="insRow()" /></td>
        <td><img alt="Icon" src="/assets/minus-icon.png" id="delPOIbutton" onclick="deleteRow(this)"/></td>
    </tr>

    <tr>
        <td><input size=25 type="text"/></td>
        <td><input size=25 type="text"/></td>
        <td><input size=25 type="text"/></td>
        <td><input size=25 type="text"/></td>
        <td><input size=25 type="text"/></td>
        <td><img alt="Icon" src="/assets/add-icon.png" id="addmorePOIbutton" onclick="insRow()" /></td>
        <td><img alt="Icon" src="/assets/minus-icon.png" id="delPOIbutton" onclick="deleteRow(this)"/></td>
    </tr>
</table>

Now my javascript code to put the ids of the input text box is: 现在,我用于输入输入文本框ID的JavaScript代码是:

  function make_summary()
  {
    var arr=new Array();
    var x=document.getElementById('POITable');
    var size=x.rows.length;

    for (i=2;i<=x.rows.length-1;i++)
    {
      var each_row=x.rows[i];
      var each_col=each_row.cells[1];
      console.log(each_col);   
    }
  }

I can access each table data, I mean the td tag, But since there is <input> tag inside each td tag,so how to access that input tag element? 我可以访问每个表数据,我的意思是td标签,但是由于每个td标签中都有<input>标签,那么如何访问该输入标签元素?

each_col.getElementByTagName(); is not working. 不管用。 It is throwing error. 它抛出错误。

Please give me some solutions. 请给我一些解决方案。 Please give me only javascript solutions (no jquery please). 请只给我JavaScript解决方案(请不要使用jquery)。

As there's always only one element in your td tags, you can just access the inner element with 由于td标签中始终只有一个元素,因此您可以使用以下命令访问内部元素

each_col.getElementsByTagName('input')[0];

Remember that getElementsByTagName() returns a HtmlCollection, not a single element - so you have to specify which of the elements you want to target. 请记住, getElementsByTagName()返回的是HtmlCollection,而不是单个元素-因此,您必须指定要定位的元素。

BTW... There's no method called getElementByTagName() - this might be where the confusion comes from. 顺便说一句...没有名为getElementByTagName()方法-这可能是造成混乱的地方。 It's the plural of Element -> Elements 这是Element-> Elements的复数

getElementsByTagName()
          ^ 

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

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