簡體   English   中英

如何在javascript中獲取html表的行數據

[英]How to get row data of a html table in javascript

我有一張這樣的桌子

<tr class="odd">
    <td class="  sorting_1">opps</td>
    <td class="center "> <a href="#" onclick="tst(this)" class="btn btn-warning">
    <i class="icon-edit icon-white">      
    </i> Edit</a>

    </td>
</tr>

點擊該超鏈接我需要該行的td值。

這個javascript我試過了

 $(document).ready(function () {
     $("#tblTest td:nth-child(2)").click(function (event) {
         //Prevent the hyperlink to perform default behavior
         event.preventDefault();
         var $td = $(this).closest('tr').children('td');
         var sr = $td.eq(0).text();
         alert(sr);
     });
 });

如果我放入表單擊事件,這個工作。 我需要點擊該超鏈接它應該發生。 怎么做到這一點? 提前致謝。

沒有逐行迭代,如果有任何方法可以得到它?

要綁定click事件, td將其綁定到a

更改

$("#tblTest td:nth-child(2)").click(function (event) {

$("#tblTest td:nth-child(2) a").click(function (event) {

嘗試這個:

$("a.btn").click(function (event) {
    //Prevent the hyperlink to perform default behavior
    event.preventDefault();
    var $td = $(this).parent().closest('tr').children('td');
    var sr = $td.eq(0).text();
    alert(sr);
});

在這里演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM