【问题标题】:Show cell values into textboxes from HTML table MySQL data将 HTML 表 MySQL 数据中的单元格值显示到文本框中
【发布时间】:2016-11-20 16:23:06
【问题描述】:

当点击特定行时,有什么方法可以将 html 表格中的单元格值显示到文本框?

          $result=mysqli_query($conn, $sql1);
          echo "<table class='data'>
              <tr>
                  <th>Agent ID</th>
                  <th>Fullname</th>
                  <th>Address</th>
                  <th>Gender</th>
                  <th>Contact</th>

              </tr>";

          while($row=mysqli_fetch_assoc($result))
          {
              echo "<tr>";
              echo "<td>". $row['id']. "</td>";
              echo "<td>". $row['fullname']. "</td>";
              echo "<td>". $row['address']. "</td>";
              echo "<td>". $row['gender']. "</td>";
              echo "<td>". $row['contact']. "</td>";
              echo "</tr>";
          }  
          echo "</table>";
  ?>

【问题讨论】:

标签: javascript php html mysql


【解决方案1】:

您可以使用 jQuery 通过 on("click") 事件处理程序来执行此操作。

(见 cmets)

$("td").on("click", function() {

  /* create a new input element */

  var new_edit = document.createElement("input");
  $(new_edit).prop("type", "text");

  /* set value to whatever was in <td>  */
  $(new_edit).val($(this).text()); 

  /* insert new input into <td>  and focus on the new input */
  $(this).html(new_edit);

  $(new_edit).focus();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table width="50%" border="1px solid black">
  <th>col 1</th>
  <th>col 2</th>
  <tr>
    <td>value 1</td>
    <td>value 2</td>
  </tr>
  <tr>
    <td>value 1</td>
    <td>value 2</td>
  </tr>
  <tr>
    <td>value 1</td>
    <td>value 2</td>
  </tr>

</table>

【讨论】:

    猜你喜欢
    • 2016-11-10
    • 2012-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-24
    • 1970-01-01
    相关资源
    最近更新 更多