繁体   English   中英

从表中获取单元格值

[英]getting cell value from a table

我有一个问题,我有一个脚本,可以在从数据库中获取值时打印表格,现在,我的问题是我希望每个单元格的值都可单击,我知道如何做到这一点,我添加了

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

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

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

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

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

(编辑如下)

要么

使用jQuery [ DEMO ]

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

您可以使用class轻松识别td 否则,您可以使用$(td).click()

在表格的每个单元格中添加一个类(例如“ click_td”)。 然后是JS。

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

 });​

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

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

为了使用数据库数据调用该函数,您可以执行以下操作:

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

使用jQuery会更加容易!

<a href="sample.php?id=<?php echo $row['value']; ?>"><?php echo $row['value']; ?></a>

您的问题并不模糊,但是信息很少,例如,您要将每个单元格的标识符保留在哪里? 它会在隐藏的输入字段中,还是会出现在链接甚至是javascript函数中? 无论哪种方式,这都是您要执行的操作:

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>';

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM