How do I get data from a table?

后端 未结 3 1010
一整个雨季
一整个雨季 2020-11-27 17:25

How do I pull data (string) from a column called "Limit" in a table ("displayTable") in Javascript?

var table = document.getElementById(\'         


        
相关标签:
3条回答
  • 2020-11-27 18:06

    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

    //gets table
    var oTable = document.getElementById('myTable');
    
    //gets rows of table
    var rowLength = oTable.rows.length;
    
    //loops through rows    
    for (i = 0; i < rowLength; i++){
    
       //gets cells of current row
       var oCells = oTable.rows.item(i).cells;
    
       //gets amount of cells of current row
       var cellLength = oCells.length;
    
       //loops through each cell in current row
       for(var j = 0; j < cellLength; j++){
          /* 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>
        //gets table
        var oTable = document.getElementById('myTable');
    
        //gets rows of table
        var rowLength = oTable.rows.length;
    
        //loops through rows    
        for (i = 0; i < rowLength; i++){
    
          //gets cells of current row  
           var oCells = oTable.rows.item(i).cells;
    
           //gets amount of cells of current row
           var cellLength = oCells.length;
    
           //loops through each cell in current row
           for(var j = 0; j < cellLength; j++){
    
                  // get your cell info here
    
                  var cellVal = oCells.item(j).innerHTML;
                  alert(cellVal);
               }
        }
    </script>
    
    0 讨论(0)
  • 2020-11-27 18:11

    in this code data is a two dimensional array of table data

    let oTable = document.getElementById('datatable-id');
    let data = [...oTable.rows].map(t => [...t.children].map(u => u.innerText))
    
    0 讨论(0)
  • 2020-11-27 18:12

    use Json & jQuery. It's way easier than oldschool javascript

    function savedata1() { 
    
    var obj = $('#myTable tbody tr').map(function() {
    var $row = $(this);
    var t1 = $row.find(':nth-child(1)').text();
    var t2 = $row.find(':nth-child(2)').text();
    var t3 = $row.find(':nth-child(3)').text();
    return {
        td_1: $row.find(':nth-child(1)').text(),
        td_2: $row.find(':nth-child(2)').text(),
        td_3: $row.find(':nth-child(3)').text()
       };
    }).get();
    
    0 讨论(0)
提交回复
热议问题