简体   繁体   English

如何在javascript中获取html表的行数据

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

I have a table like this 我有一张这样的桌子

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

On click of that hyperlink i need the td values of the row. 点击该超链接我需要该行的td值。

This javascript i tried 这个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);
     });
 });

This one works if i put in Table click event. 如果我放入表单击事件,这个工作。 I need on click of that hyperlink it should happen. 我需要点击该超链接它应该发生。 How to achieve this ? 怎么做到这一点? Thanks in Advance. 提前致谢。

without iterating row by row if any method is there to get it ? 没有逐行迭代,如果有任何方法可以得到它?

You are binding click event to td bind it to a 要绑定click事件, td将其绑定到a

Change 更改

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

To

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

Try this: 尝试这个:

$("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);
});

DEMO HERE 在这里演示

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

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