簡體   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