0

我有一个问题,我有一个脚本可以在从数据库中获取值的同时打印一个表格,现在,我的问题是我想要每个单元格的值可点击,我知道如何做到这一点,我补充说

<a href="sample.php">row from query result is printed here</a>

每次在单元格中打印一个值

现在,每个单元格都有不同的值,当点击者点击其中一个值时,我不知道如何获得用户点击的单元格的值,有什么建议吗?

4

5 回答 5

4
<a href="sample.php?value=<?php echo $yourvalue?>">row from query result is printed here</a>

您可以将值作为 GET 变量传递。

(以下编辑)

或者

使用 jquery [演示]

$(".tdclass").click(function(){
   alert($(this).text());
   //OR 
   //alert($(this).html());
});​

td您可以使用类轻松识别。否则你可以使用$(td).click().

于 2012-09-26T13:06:32.497 回答
0

向表格的每个单元格添加一个类(比如“click_td”)。然后做这个就是JS。

     $(document).ready(function() {
      $('#table_1 .click_td').click(function(){
     alert($(this).text());
  });

 });​
于 2012-09-26T13:05:14.557 回答
0

我假设您想用 javascript 处理数据。因此,在 javascript 中创建一个函数:

function handle_db_data(var data){...do whatever you want here...}

And in order to call that function with the database data, you can do this:

<a name="row1" onclick="handle_db_data(ROW_FROM_DATABASE);">ROW_FROM_DATABASE</a>

Using jQuery would be even more easy!

于 2012-09-26T13:08:01.277 回答
0
<a href="sample.php?id=<?php echo $row['value']; ?>"><?php echo $row['value']; ?></a>
于 2012-09-26T13:09:57.613 回答
0

Your question is not vague but has little information, for instance, where do you want to keep the identifier of each cell? will it be in a hidden input field or will it be in a the link or even a javascript function? Either way this would be how you want to do this:

echo '<table>';
$counter = 1;

/* Assuming the database output is an associative array. */
foreach($db_data as $item) {
   echo '<tr>';
   echo '<td>'.$counter.'</td>';
   /* if the row identifier will be in your link. */
   echo '<td><a href="sample.php?rowid='.$item['pk_row_id'].'">row from query result is printed here</a></td>';
   /* if the row identifier will be a hidden input field. */
   echo '<td><input type="hidden" value="'.$item['pk_row_id'].'" name="rowid" id="rowid" />';
   /* if the row identifier will be in a javascript function on click. */
   echo '<td><a href="void(0)" onclick="userclickaction('.$item['pk_row_id'].')>Click Me to edit</a></td>`enter code here`';
   echo '</tr>';

   /* Increment counter. */
   $counter++; 
}

echo '</table>';
于 2012-09-26T13:16:55.737 回答