javascript - 如何将 <tr> 插入到 <table> 中(使用 PHP)

标签 javascript php html

我正在使用 PHP,并且我有一个想要动态填充的表。

<table>
    <tr>
         <th>This One</th>
         <th>That One</th>
         <th>The Other One</th>
    </tr>
    <tr>
         <td>Final This</td>
         <td>Final That</td>
         <td>Final The Other</td>
   </tr>
</table>

我正在填充表数据 onchange,并且我希望将数据添加到上面 标有“Final blah”的行。

每个新行都有数据填充每个单元格。

是否有一些功能可以使用 PHP 来完成此操作,也许还可以利用一些 JavaScript?

编辑: 我想要得到如下所示的最终产品:

<table>
    <tr>
         <th>This One</th>
         <th>That One</th>
         <th>The Other One</th>
    </tr>
    <tr> Dynamically added row. Cells already populated from database</tr>
    <tr> Next row of data from the database</tr>
    <tr> The next row should **ALWAYS** be the last row</tr>
    <tr>
         <td>Final This</td>
         <td>Final That</td>
         <td>Final The Other</td>
   </tr>
</table>

请注意,我想保留一个标题行!
我想在标题行和带有“最终内容”的行之间插入行。 最后一行始终存在。

最佳答案

如果我收到您的询问:

 <table id="myTable">
  <tr>
     <th>This One</th>
     <th>That One</th>
     <th>The Other One</th>
  </tr>
   <tbody id="myid">
    <tr><td>Final This</td><td>Final That</td><td>This is your Last Row</td></tr>
   </tbody>
    </table>
    <br>

<button onclick="myCreateFunction()">Add row</button>

<script>
function myCreateFunction() {
    var table = document.getElementById("myid");
    var row = table.insertRow(0);
    var cell1 = row.insertCell(0);
    var cell2 = row.insertCell(1);
    var cell3 = row.insertCell(2);
    cell1.innerHTML = "Final This";
    cell2.innerHTML = "Final That";
    cell3.innerHTML = "I am inserted between First and last row";
}

</script>

Fiddle

关于javascript - 如何将 <tr> 插入到 <table> 中(使用 PHP),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47447710/

相关文章:

javascript - 按F5(刷新)后如何运行函数?

javascript - Ng-repeat 在将值插入数组后不更新,尝试了 $scope.$apply() 但返回错误

javascript - 为什么 javascript 中的 switch case 不适用于 Microsoft Edge 上的字符串?

php - 方法、常量、变量和字段的奇异名称 - 错误还是功能?

PHP preg_replace() 改变版权年份

php - 自定义 session_handler 被 php7 破坏(session_set_save_handler)

javascript - 从 html 页面到 PHP 页面的 anchor 链接

javascript - 如何在 Javascript 中处理 Ajax 生成的表单?

html - iframe 可以使用父页面中的资源吗?

javascript - Wordpress 帖子/页面编辑器无法工作 - 无法读取属性错误