【问题标题】:Trying to get value from table on click试图在点击时从表中获取价值
【发布时间】:2020-11-23 04:53:03
【问题描述】:

我有一张这样的桌子:

<table class="table" id = "posts_table">

  <caption><h2>cars</h2></caption>
  <tr class="success">
    <th style="text-align:center"> post_id 
    <th style="text-align:center"> autor 
    <th style="text-align:center"> title 
  </tr>
  <?php 
    $cursor = $MySQLdb->prepare("SELECT * FROM posts WHERE topic_id=:topic_id");
    $cursor->execute( array(":topic_id"=>"1") ); //לשנות
    foreach ($cursor->fetchAll() as $obj): ?>
    <tr>
      <td style="text-align:center"><? echo $obj['post_id'] ?></td>
      <td style="text-align:center"><? echo $obj['full_name']?></td>
      <td style="text-align:center"><a href="post.php#"><? echo $obj['post_title']?></a></td>
    </tr>
  <? endforeach; ?>    
</table

我想创建一个函数,当你点击标题时,我会得到post_id 的原始数据。我应该怎么做才能实现它?

【问题讨论】:

  • 在 SO 上发布“为我编写所有代码”问题通常不是一个好主意。如果您包含您尝试过的代码以及对哪些代码不起作用的说明,您就更有可能获得有用的答案。
  • 你希望它在点击标题时返回 post_id 吗?不太清楚被问到什么 x:

标签: javascript php html sql


【解决方案1】:

所以我尝试执行此代码并且它有效:

<table class="table" id = "posts_table">

            <caption><h2>cars</h2></caption>
            <tr class="success">
                <th style="text-align:center"> post_id </th>
                <th style="text-align:center"> autor </th>
                <th style="text-align:center"> title </th>
            </tr>
            <?php
                $cursor = $MySQLdb->prepare("SELECT * FROM posts WHERE topic_id=:topic_id");
                $cursor->execute( array(":topic_id"=>"1") ); //לשנות
                foreach ($cursor->fetchAll() as $obj): ?>
            <tr>
                 <td class="post_id_c" style="text-align:center"><? echo $obj['post_id'] ?></td>
                 <td style="text-align:center"><? echo $obj['full_name']?></td>
                 <td style="text-align:center"><a class="click_title"><? echo $obj['post_title']?></a></td>
            </tr>
                <? endforeach; ?>    
          </table> 
          <script>
              $(".click_title").click(function() {
                        var $item = $(this).closest("tr")   // Finds the closest row <tr> 
                       .find(".post_id_c")     // Gets a descendent with class="nr"
                       .text(); 
                  console.log($item);
              });
            
          </script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-04
    • 2016-05-29
    • 2018-12-09
    • 1970-01-01
    • 2014-03-02
    • 1970-01-01
    • 2016-12-20
    • 1970-01-01
    相关资源
    最近更新 更多