使用 PHP 和 Jquery 在模态文本框中显示值

我正在尝试使用 PHP 和 JQuery 将 SQL 数据库中的值显示到文本框中。正在检索这些值,但不会显示在文本框中。我究竟做错了什么?


<?php


require 'UpdateTimerSQL.php';


?>



<!DOCTYPE html>

<html>

 <head>

  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

  <title>LiveData</title>

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

  <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />

 </head>

 <body>


<div class="container">

<h2>Ajax</h2>

<p>Update Timer</p>

<table class="table">

<?php

    $table = mysqli_query($connection, 'SELECT * FROM Timers');

    while($row = mysqli_fetch_array($table)){ 

      ?>

        <tr id="<?php echo $row['ID']; ?>">

            <th>Timer_1</th>

            <td data-target="Timer_1"><?php echo $row['Timer_1']; ?></td>

              <th> 

            <a href="#" data-role="update" class="btn btn-primary" data-id="<?php echo $row['ID']; ?>">Update</a> 

             </th> 

        </tr>

     <?php

          } 

     ?>

    </table>


    <div class="container">



  <div class="text-center"></div>


</div>


<!-- Modal -->

<div id="myModal" class="modal fade" role="dialog">

  <div class="modal-dialog">

    <!-- Modal content-->

    <div class="modal-content">

      <div class="modal-header">

        <button type="button" class="close" data-dismiss="modal">&times;</button>

        <h4 class="modal-title">Modal Header</h4>

      </div>

      <div class="modal-body">

        <div class="form-group">

          <label>Neutral Timer 1</label>

          <input type="hidden" id="ids" class="form-control">

          <input type="text" id="Timer_1" class="form-control">


        </div>

        <div class="modal-footer">

          <a href="#" id="save" class="btn btn-primary pull-right">Update</a>

          <button type="button" class="btn btn-default pull-left" data-dismiss="modal">Close</button>

        </div>

      </div>

    </div>

  </div>



天涯尽头无女友
浏览 97回答 1
1回答

梵蒂冈之花

您的表结构无效。此外,您Timer_1无法找到所需的文本,这就是它无法正常工作的原因。您的PHP代码应如下所示:&nbsp;<table class="table"><?php&nbsp; &nbsp; $table = mysqli_query($connection, 'SELECT * FROM Timers');&nbsp; &nbsp; while($row = mysqli_fetch_array($table)){&nbsp;&nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; <tr id="<?php echo $row['ID']; ?>">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Timer 1</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td data-target="Timer_1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php echo $row['Timer_1']; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="#" data-role="update" class="btn btn-primary" data-id="<?php echo $row['ID']; ?>">Update</a>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</th>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp;<?php&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp;?>&nbsp; &nbsp; </table>这是演示代码:$(document).ready(function() {&nbsp; $(document).on('click', 'a[data-role=update]', function() {&nbsp; &nbsp; var id = $(this).data('id');&nbsp; &nbsp; //this(a tag)->tr(id)->td(data attr)->text&nbsp; &nbsp; var Timer_1 = $(this).closest('tr[id=' + id + ']').find("td[data-target='Timer_1']").text();&nbsp; &nbsp; //putting value in input box&nbsp; &nbsp; $("#ids").val(id)&nbsp; &nbsp; $('#Timer_1').val(Timer_1);&nbsp; &nbsp; $('#myModal').modal('toggle');&nbsp; });&nbsp; //onclick of update button&nbsp; $(document).on('click', '#save', function() {&nbsp; &nbsp; //getting value of input box&nbsp; &nbsp; var input_val = $(this).closest(".modal-body").find("#Timer_1").val();&nbsp; &nbsp; //getting hidden id&nbsp;&nbsp; &nbsp; var ids = $(this).closest(".modal-body").find("#ids").val();&nbsp; &nbsp; //finding tr with same id and then update the td&nbsp; &nbsp; $("table").find("tr[id='" + ids + "']").find("td[data-target='Timer_1']").text(input_val);&nbsp; &nbsp; $('#myModal').modal('toggle');&nbsp; });});<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script><script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script><link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" /><div class="container">&nbsp; <h2>Ajax</h2>&nbsp; <p>Update Timer</p>&nbsp; <table class="table">&nbsp; &nbsp; <tr id="1">&nbsp; &nbsp; &nbsp; <th>Timer 1</th>&nbsp; &nbsp; &nbsp; <td data-target="Timer_1"> Something</td>&nbsp; &nbsp; &nbsp; <th>&nbsp; &nbsp; &nbsp; &nbsp; <a href="#" data-role="update" class="btn btn-primary" data-id="1">Update</a>&nbsp; &nbsp; &nbsp; </th>&nbsp; &nbsp; </tr>&nbsp; &nbsp; <tr id="2">&nbsp; &nbsp; &nbsp; <th>Timer 1</th>&nbsp; &nbsp; &nbsp; <td data-target="Timer_1"> Something123</td>&nbsp; &nbsp; &nbsp; <th>&nbsp; &nbsp; &nbsp; &nbsp; <a href="#" data-role="update" class="btn btn-primary" data-id="2">Update</a>&nbsp; &nbsp; &nbsp; </th>&nbsp; &nbsp; </tr>&nbsp; </table>&nbsp; <div class="text-center"></div></div><!-- Modal --><div id="myModal" class="modal fade" role="dialog">&nbsp; <div class="modal-dialog">&nbsp; &nbsp; <!-- Modal content-->&nbsp; &nbsp; <div class="modal-content">&nbsp; &nbsp; &nbsp; <div class="modal-header">&nbsp; &nbsp; &nbsp; &nbsp; <button type="button" class="close" data-dismiss="modal">&times;</button>&nbsp; &nbsp; &nbsp; &nbsp; <h4 class="modal-title">Modal Header</h4>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; <div class="modal-body">&nbsp; &nbsp; &nbsp; &nbsp; <div class="form-group">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label>Timer 1</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="hidden" id="ids" class="form-control">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" id="Timer_1" class="form-control">&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="modal-footer">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="#" id="save" class="btn btn-primary pull-right">Update</a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button type="button" class="btn btn-default pull-left" data-dismiss="modal">Close</button>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div>&nbsp; </div>
打开App,查看更多内容
随时随地看视频慕课网APP