繁体   English   中英

如何使用JavaScript从HTML表格的单元格中获取值

[英]How to grab the values from an HTML table's cells using JavaScript

我正在尝试从HTML表中获取单元格值,以便通过调用PHP将它们保存到MySQL表中。 我想使用JavaScript而不是jQuery。

我根据MySQL表的ID字段给每个TR一个不同的ID。 此外,每个TD输入单元具有基于MySQL表的ID字段的唯一ID。 不确定我是否需要所有这些,但我使用它们。

我如何获取单元格的值,以便我可以将它传递给PHP程序(我知道如何编写这个PHP)以将数据保存到MySQL中? 我下面的JavaScript代码抓住了“innerHTML”,但我需要获取单元格值。

例如,如果我有一个来自MySQL表的3行数据,包含字段id和amount,以及下面的值,我如何获取“3”和“1.99”?:

id    amount
-------------
1     100.00
2     9.99
3     1.99

下面是表格,提交按钮和onclick调用的PHP / HTML代码示例:(这是我实际代码的一个非常简化的版本,但应该传达这个想法)

<?php
    /* define mysql database connect, query and execute it returning result set into $result, id=integer primary key, is_active=tinyint(0 or 1), amount=decimal(12,2) */
    /* ... */    
    /* output form/table */
    echo "<form id='myform' name='myform' >" ;    
        echo "<table id='mytable' name='mytable'>" ;
            while($row = mysql_fetch_array($result))
            {
                $id                 = $row['id'] ;
                $amount             = $row['amount'] ;                
                $tr_id              = "tr_id_" . trim((string)$id) ;                                                
                $td_id              = "td_id_" . trim((string)$id) ;  
                $td_amount_id       = "td_amount_id_" . trim((string)$id) ;                 
                $input_amount_id    = "input_amount_id_" . trim((string)$id) ;
                echo "<tr id='$tr_id' name='$tr_id'>";
                    echo "<td id='$td_id_account' > $id </td>" ; 
                    echo "<td id='$td_amount_id' > <input type='text' id='$input_amount_id' value=$amount > $amount </td>" ;             
                echo "</tr>";
            }
        echo "</table>";
    echo "</form>" ; 
    /* submit button and onclick call */
    echo "<br />" ;
    echo "<table>";
        echo "<tr>" ;
            echo "<td>";
                echo "<button type='button' " . 'onclick="SubmitTableData(\'' . "mytable" .'\');"' . ">Submit</button>" ;
            echo "</td>" ;
        echo "</tr>";
    echo "</table>";         
?>

这是我的JavaScript函数示例,用于循环遍历HTML表的各行:

function SubmitTableData(TableID)
{
    var table = document.getElementById(TableID);
    for (var i = 0, row; row = table.rows[i]; i++) 
    {
       // iterate through rows
       for (var j = 0, col; col = row.cells[j]; j++) 
       {
         // iterate through columns
            alert(col.innerHTML);
       }  
       /* call PHP procedure with row's cell values as parameters */
       /* ... */
       break;
    }
} 

使用tdinnerText属性。 这是一个显示如何使用它的小提琴

HTML

<table>
    <tr>
        <td id="1">Bla</td>
        <td id="2"><input id="txt" />Ble</td>
    </tr>
</table>​

JavaScript的

var td = document.getElementById('1');
alert(td.innerText);
var td2 = document.getElementById('2');
alert(td2.innerText);​

如何从表中获取单元格值?

更新以解决艾略特的评论

了解如何我的新演示中 获取单元格值 (innerText和data-value)

在表中,属性data-value用于存储一些数据(2B,3C,..)。

<table id="t2">    
  <thead>
   <tr id="tr1"><th>Student Name<th>Course</tr> 
  </thead>
  <tbody>
    <tr id="tr2"><td data-value="2B">Bert2  <td>Economics  </tr>
    <tr id="tr3"><td data-value="3C">Clyde3 <td>Chemics    </tr>
    <tr id="tr4"><td data-value="4D">       <td>Digital Art</tr>
    <tr id="tr5"><td data-value="5E">Ernest <td>Ecmoplasma </tr>
  </tbody>
</table> 

使用jQuery和正确的选择器,您可以迭代每个单元格:

function eachCell(){
    var cellInnerText = [];
    var cellValue = [];
    var out = document.getElementById("out");
    var out2 = document.getElementById("out2");
    // iterate over each row in table with id t2 in the table-body (tbody)
    $('#t2 tbody tr').each(function(index){     
       // copy the text into an array
       cellInnerText.push($(":first-child", $(this)).text());
       //copy the data-value into an array
       cellValue.push($(":first-child", $(this)).attr('data-value'));
    });
    // show content of both arrays
    out.innerHTML = cellInnerText.join(" | ");
    out2.innerHTML = cellValue.join(" | "); 
}

使用某种模式在您的输入上放置ID。 例如<input type="text" id="input_0_0" name="whatever" />其中0表示行,第二个0表示列,然后是警报类型

v = document.getElementById('input_'+row+'_'+col).value;

它应该有助于你现在滚动

提醒表的第一行中第一个单元格的innerHTML

<!DOCTYPE html>
<html>
<head>
<style>
table, td {
    border: 1px solid black;
}
</style>
</head>
<body>

<p>Click the button to alert the innerHTML of the first cell (td element with index 0) in the table's first row.</p>

<table id="myTable">
  <tr>
    <td>Row1 cell1</td>
    <td>Row1 cell2</td>
  </tr>
  <tr>
    <td>Row2 cell1</td>
    <td>Row2 cell2</td>
  </tr>
  <tr>
    <td>Row3 cell1</td>
    <td>Row3 cell2</td>
  </tr>
</table>
<br> 

<button onclick="myFunction()">Try it</button>

<script>
function myFunction() {
    alert(document.getElementById("myTable").rows[0].cells[0].innerHTML);
}
</script>

</body>
</html> 

暂无
暂无

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

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