[英]getting cell value from a table
i am having a problem, i have a script that prints a table while getting values from the database, now, my problem is i want the values of each cell clickable, i know how to make that, i added 我有一个问题,我有一个脚本,可以在从数据库中获取值时打印表格,现在,我的问题是我希望每个单元格的值都可单击,我知道如何做到这一点,我添加了
<a href="sample.php">row from query result is printed here</a>
everytime it prints a value in the cell 每次在单元格中打印一个值
now, each cell has different values, and when ever a clicker clicks one of the values i have no idea on how will i get the value of the cell that the user clicked, any suggestions?? 现在,每个单元格都有不同的值,每当单击器单击这些值之一时,我都不知道如何获得用户单击的单元格的值,有什么建议吗?
<a href="sample.php?value=<?php echo $yourvalue?>">row from query result is printed here</a>
You can pass value as a GET variable. 您可以将值作为GET变量传递。
(edited below) (编辑如下)
OR 要么
using jquery [ DEMO ] 使用jQuery [ DEMO ]
$(".tdclass").click(function(){
alert($(this).text());
//OR
//alert($(this).html());
});
You can easily identify td
using class. 您可以使用class轻松识别td
。 Oterwise you can use $(td).click()
. 否则,您可以使用$(td).click()
。
add a class (say 'click_td') to each cell of the table. 在表格的每个单元格中添加一个类(例如“ click_td”)。 and then do this is JS. 然后是JS。
$(document).ready(function() {
$('#table_1 .click_td').click(function(){
alert($(this).text());
});
});
I asume you want to treat the data with javascript. 我假设您想使用javascript处理数据。 So, create a function in 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! 使用jQuery会更加容易!
<a href="sample.php?id=<?php echo $row['value']; ?>"><?php echo $row['value']; ?></a>
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? 它会在隐藏的输入字段中,还是会出现在链接甚至是javascript函数中? 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>';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.