【问题标题】:Jquery loop through table rows and get nth child valueJquery遍历表行并获取第n个子值
【发布时间】:2016-07-23 16:57:30
【问题描述】:

我正在遍历表的行并尝试获取第一个孩子的值。但不知何故,它一直只返回第一行的值

    $("#lookupquicksearchBtn").on("click",function(){
        var searchWord = $("#quickSearchTextBox").val();

        $("table tr").each(
          function(){ 
             alert($("td:nth-child(1)").html());             
          });
    });

它总是以第一行第一列的值发出警报。

【问题讨论】:

    标签: jquery jquery-selectors html-table


    【解决方案1】:

    您需要在此处使用当前行上下文以及查找选择器:

      $("table tr").each(function(){ 
        alert($(this).find("td:nth-child(1)").html());             
      });
    

    【讨论】:

      【解决方案2】:

      试试下面的代码。

      $('table tr').each(function(i,e){
          alert($(this).find("td:nth-child(1)").html())
      })
      

      您可以使用i 来获取索引。

      【讨论】:

        猜你喜欢
        • 2016-12-09
        • 2012-05-13
        • 2020-02-12
        • 2015-09-21
        • 2020-10-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-23
        相关资源
        最近更新 更多