【问题标题】:Get <td> value on <td> click event在 <td> 点击事件上获取 <td> 值
【发布时间】:2018-08-27 00:53:39
【问题描述】:

我已经使用 jQuery 成功实现了&lt;table&gt; &lt;tr&gt; 点击事件,其中我使用以下 .taglib-search-iterator 获取所选行的所需&lt;td&gt; 的值是我的表class

$(".taglib-search-iterator tbody").on("click", "tr", function(e){
     alert($(this).find("td").eq().html());});

但根据我的要求,我需要忽略 table 的第一行 &lt;tr&gt;倒数第二个last&lt;td&gt;的表。为此,我做了以下工作,

    $(".taglib-search-iterator tbody tr:not(:first-child) td:not(:nth-last-child(2))").click(function(){
    alert('Right position clicked');
});

现在我的问题是当点击事件被触发时,我无法在第 7 列检索&lt;td&gt; 的值。

我已经关注了

alert($(this).eq(7).html());

但它返回 undefined。我也尝试过简单地使用

alert($(this).html());

它正在返回我点击的&lt;td&gt; 的值。

问题?

如何检索特定&lt;td&gt; 的值?

添加,&lt;td&gt; ,&lt;tr&gt; 是自动生成的,因此它与动态 classid 相关联,因此无法使用。

【问题讨论】:

    标签: javascript jquery html-table


    【解决方案1】:

    使用.closest(selector) 获取与selector 匹配的最近祖先

    alert($(this).closest("tr").children("td").eq(7).html());
    

    【讨论】:

      猜你喜欢
      • 2015-11-13
      • 1970-01-01
      • 1970-01-01
      • 2011-05-18
      • 1970-01-01
      • 2011-03-28
      • 1970-01-01
      • 2014-06-23
      • 2011-11-25
      相关资源
      最近更新 更多