zoukankan      html  css  js  c++  java
  • How do I get data from a data table in javascript?

    This is how I accomplished reading a table in javascript. Basically I drilled down into the rows and then I was able to drill down into the individual cells for each row. This should give you an idea

    var oTable = document.getElementById('myTable');
    //gets table

    var rowLength = oTable.rows.length;
    //gets rows of table

    for (i = 0; i < rowLength; i++){
    //loops through rows

    var oCells = oTable.rows.item(i).cells;
    //gets cells of current row
    var cellLength = oCells.length;
    for(var j = 0; j < cellLength; j++){
    //loops through each cell in current row
    <!--get your cell info here-->
    <!--var cellVal = oCells.item(j).innerHTML;-->
    }
    }
    UPDATED - TESTED SCRIPT

    <table id="myTable">
    <tr>
    <td>A1</td>
    <td>A2</td>
    <td>A3</td>
    </tr>
    <tr>
    <td>B1</td>
    <td>B2</td>
    <td>B3</td>
    </tr>
    </table>
    <script>
    var oTable = document.getElementById('myTable');
    //gets table

    var rowLength = oTable.rows.length;
    //gets rows of table

    for (i = 0; i < rowLength; i++){
    //loops through rows

    var oCells = oTable.rows.item(i).cells;
    //gets cells of current row
    var cellLength = oCells.length;
    for(var j = 0; j < cellLength; j++){
    //loops through each cell in current row
    <!--get your cell info here-->
    var cellVal = oCells.item(j).innerHTML;
    alert(cellVal);
    }
    }
    </script>

  • 相关阅读:
    1001 字符串“水”题(二进制,map,哈希)
    hzau 1210 Happiness
    hzau 1209 Deadline(贪心)
    hzau 1208 Color Circle(dfs)
    hzau 1207 Candies
    hzau 1206 MathematicalGame
    hzau 1205 Sequence Number(二分)
    hzau 1204 Escape from the Darkness
    Calculator 2
    Calculator
  • 原文地址:https://www.cnblogs.com/zhangchenliang/p/3041016.html
Copyright © 2011-2022 走看看