简体   繁体   English

如何从表中获取数据?

[英]How do I get data from a table?

How do I pull data (string) from a column called "Limit" in a table ("displayTable") in Javascript?如何从 Javascript 中的表(“displayTable”)中名为“Limit”的列中提取数据(字符串)?

var table = document.getElementById('displayTable');    
var rowCount = table.rows.length;    
for (var i = 1; i < rowCount - 1; i++) {    
     var row = table.rows[i]["Limit"].ToString();
     alert(row);
     ...
}

This is how I accomplished reading a table in javascript.这就是我在 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>

in this code data is a two dimensional array of table data在此代码中data是表数据的二维数组

let oTable = document.getElementById('datatable-id');
let data = [...oTable.rows].map(t => [...t.children].map(u => u.innerText))

use Json & jQuery.使用 Json 和 jQuery。 It's way easier than oldschool javascript它比 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();

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

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