现在的位置: 首页 > 综合 > 正文

How do I get data from a data table in javascript?

2012年11月26日 ⁄ 综合 ⁄ 共 1056字 ⁄ 字号 评论关闭

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>

抱歉!评论已关闭.