javascript - 试图在单击时获取链接以在 div 中显示数据?

标签 javascript php jquery html pdo

我不确定为什么这不起作用。当我单击链接时,数据应该显示在 div 中,但由于某种原因什么也没有显示。有人对为什么会这样有任何建议吗?我不确定我的脚本是否设置正确。

 <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="description" content="">
        <meta name="author" content="">
        <link rel="icon" href="favicon.ico">
        <title>test</title>
        <!-- Bootstrap core CSS -->
        <link href="../css/bootstrap.css" rel="stylesheet">
        <link href="../css/custom.css" rel="stylesheet">

        <script>
    $('a').on('click', function() {
        var div_id = $(this).data('id');

        $('.toggle_content').hide();
        $('#' + div_id).toggle();
    });
    </script>
      </head>
      <body>

      <nav>

       <a href="#" target="_top" data-id="one">Received</a>
       <a href="#" target="_top" data-id="one">
      <?php 
       //conects to the database
        require_once("../db_connect.php");
      $stmt = $db->prepare ("SELECT COUNT(*) AS rows_cnt FROM receivingrequests WHERE status='Received'");
      $stmt->execute();
      while ($row = $stmt->fetch(PDO::FETCH_ASSOC))    {
        echo $row['rows_cnt'];
      }

      ?></a>


        <br>


        <a href="#" data-id="two">Info Div #2</a><br>&nbsp;<a href="#" data-id="three">Info Div #3</a> 
        <br> 
        <a href="#" data-id="four">Info Div #4</a> 
    </nav>

    <div id="one" class="toggle_content">
     <?php
        //conects to the database
        require_once("../db_connect.php");

        //prepared statement with PDO to query the database
        $stmt = $db->prepare("SELECT * FROM receivingrequests WHERE Status='RECEIVED'");
        $stmt->execute();

        ?>

        <?php //start of the while loop ?>
        <?php while( $row = $stmt->fetch(PDO::FETCH_ASSOC) ) { ?>
     <table border="1" style="border: thin #000000; table-layout: fixed; width: 100%; background-color: #FFFFFF; display: table;" class="style1">

        <tr> 
            <th style="width:15%; background-color: #000000;color: #FFFFFF;" class="style3">
            <strong>Request#</strong></th>
            <th style="width:15%; background-color: #000000;color: #FFFFFF;" class="style3">
            <strong>Status</strong></th>
            <th style="width:20%; background-color: #000000; color: #FFFFFF;" class="style3">
            <strong>Comments</strong></th>
            <th style="width:10%; background-color: #000000; color: #FFFFFF;" class="style3">
            <strong>Date Requested</strong></th>
            <th style="width:20%; background-color: #000000; color: #FFFFFF;" class="style3">
            <strong>Name</strong></th>
            <th style="width:10%;  background-color: #000000; color: #FFFFFF;" class="style3">
            <strong>Department</strong></th>
        <th style="width:10%; background-color: #000000; color: #FFFFFF;" class="style3">
        <strong>VasLblDate</strong></th>


        </tr>
        <tr>
        <?php $id = $row['RequestNumber'];?>
        <?php echo  "<td> <a href='../update.php?id=$id'>$id</a></td>"?>

            <td class="style2" style="width: 62px"><strong><?php echo $row['Status']; ?></strong></td>
            <td class="style2"><strong><?php echo $row['Comments']; ?></strong></td>
            <td class="style2"><strong><?php echo $row['DATEREQUESTED']; ?></strong></td>
            <td class="style2"><strong><?php echo $row['EmpName']; ?></strong></td>
        <td class="style2"><strong><?php echo $row['Department']; ?></strong></td>
            <td class="style2"><strong><?php echo $row['VasLbDate']; ?></strong></td>

        </tr>
        </table>
     <?php } //end of the while loop?>
    </div>


    <div id="two" class="toggle_content">Lorem ipsum... Two</div>
    <div id="three" class="toggle_content">Lorem ipsum... Three</div>
    <div id="four" class="toggle_content">Lorem ipsum... Four</div>

    <!-- Bootstrap core JavaScript
        ================================================== -->
        <!-- Placed at the end of the document so the pages load faster -->

 <script src="../javascript/jquery.js"></script>
<script src="../javascript/bootstrap.js"></script>



      </body>
    </html>

最佳答案

我使用了错误的 Jquery。我还需要将 Jquery 脚本放在页面底部。

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="favicon.ico">
    <title>test</title>

    <link href="../css/custom.css" rel="stylesheet">


  </head>
  <body background="../images/background.jpg">

  <nav>

   <a href="#" target="_top" data-id="one">Received</a>
   <a href="#" target="_top" data-id="one">
  <?php 
   //conects to the database
    require_once("../db_connect.php");
  $stmt = $db->prepare ("SELECT COUNT(*) AS rows_cnt FROM receivingrequests WHERE status='Received'");
  $stmt->execute();
  while ($row = $stmt->fetch(PDO::FETCH_ASSOC))    {
    echo $row['rows_cnt'];
  }

  ?></a>


    <br>


    <a href="#" data-id="two">Info Div #2</a><br>&nbsp;<a href="#" data-id="three">Info Div #3</a> 
    <br> 
    <a href="#" data-id="four">Info Div #4</a> 
</nav>

<div id="one" class="toggle_content">
 <?php
    //conects to the database
    require_once("../db_connect.php");

    //prepared statement with PDO to query the database
    $stmt = $db->prepare("SELECT * FROM receivingrequests WHERE Status='RECEIVED'");
    $stmt->execute();

    ?>

    <?php //start of the while loop ?>
    <?php while( $row = $stmt->fetch(PDO::FETCH_ASSOC) ) { ?>
 <table border="1" style="border: thin #000000; table-layout: fixed; width: 100%; background-color: #FFFFFF; display: table;" class="style1">

    <tr> 
        <th style="width:15%; background-color: #000000;color: #FFFFFF;" class="style3">
        <strong>Request#</strong></th>
        <th style="width:15%; background-color: #000000;color: #FFFFFF;" class="style3">
        <strong>Status</strong></th>
        <th style="width:20%; background-color: #000000; color: #FFFFFF;" class="style3">
        <strong>Comments</strong></th>
        <th style="width:10%; background-color: #000000; color: #FFFFFF;" class="style3">
        <strong>Date Requested</strong></th>
        <th style="width:20%; background-color: #000000; color: #FFFFFF;" class="style3">
        <strong>Name</strong></th>
        <th style="width:10%;  background-color: #000000; color: #FFFFFF;" class="style3">
        <strong>Department</strong></th>
    <th style="width:10%; background-color: #000000; color: #FFFFFF;" class="style3">
    <strong>VasLblDate</strong></th>


    </tr>
    <tr>
    <?php $id = $row['RequestNumber'];?>
    <?php echo  "<td> <a href='../update.php?id=$id'>$id</a></td>"?>

        <td class="style2" style="width: 62px"><strong><?php echo $row['Status']; ?></strong></td>
        <td class="style2"><strong><?php echo $row['Comments']; ?></strong></td>
        <td class="style2"><strong><?php echo $row['DATEREQUESTED']; ?></strong></td>
        <td class="style2"><strong><?php echo $row['EmpName']; ?></strong></td>
    <td class="style2"><strong><?php echo $row['Department']; ?></strong></td>
        <td class="style2"><strong><?php echo $row['VasLbDate']; ?></strong></td>

    </tr>
    </table>
 <?php } //end of the while loop?>
</div>


<div id="two" class="toggle_content">Lorem ipsum... Two</div>
<div id="three" class="toggle_content">Lorem ipsum... Three</div>
<div id="four" class="toggle_content">Lorem ipsum... Four</div>


<!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->

    <script src="http://code.jquery.com/jquery-2.1.3.min.js"></script>

   <script>
$('a').on('click', function() {
    var div_id = $(this).data('id');

    $('.toggle_content').hide();
    $('#' + div_id).toggle();
});
</script>


  </body>
</html>

  </body>
</html>

关于javascript - 试图在单击时获取链接以在 div 中显示数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27987806/

相关文章:

java - 如何使用 Jquery 或 Javascript 解析 json

javascript - 有条件地在选项标签中应用 bool 属性

javascript - Wijgrid 中是否可以有一个对象类型的单元格?

javascript - 加载网页的无 JavaScript 版本

javascript - 通过 Drive API 自动执行 Google Sheet 操作(制作副本、Google App 脚本、触发器)

php - Ajax 调用不填充表

javascript - ES6 构造函数对象参数默认值

php - 删除 order() 不起作用。 Woocommerce、wordpress

php - 在 php 中显示 MYSQL 重复错误的错误信息

javascript - Timeago + DateJS = NaN