Javascript 尝试在单击时从表中获取值
我有一张这样的桌子:Javascript 尝试在单击时从表中获取值,javascript,php,html,sql,Javascript,Php,Html,Sql,我有一张这样的桌子: <table class="table" id = "posts_table"> <caption><h2>cars</h2></caption> <tr class="success"> <th style="text-align:center"> post_id <th st
<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
汽车
邮政编码
自动
标题
因此,我尝试编写这段代码,它是有效的:
<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>
汽车
邮政编码
自动
标题
$(“.click_title”)。单击(function(){
var$item=$(this.nexist(“tr”)//查找最近的行
.find(“.post\u id\u c”)//获取class=“nr”的子代
.text();
控制台日志($项);
});
一般来说,在上面贴一个“为我编写所有代码”的问题不是一个好主意。如果你包含了你尝试过的代码和对不起作用的内容的描述,你就更有可能得到有用的答案。你想让它在你点击标题时返回帖子id吗?不太确定要问什么x: