将单元格值显示在HTML表格的文本框中MySQL数据

时间:2016-11-20 16:23:07

标签: javascript php html mysql

有没有办法在点击特定行时显示从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>";
  ?>

1 个答案:

答案 0 :(得分:0)

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

(见评论)

$("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>