循环遍历表行

时间:2013-12-10 08:12:19

标签: jquery html-table rows

我想遍历我的表行,但我总是有一个错误(无法识别的表达式)。 调用每个函数时会出现错误。

我该如何解决这个问题?

提前致谢

这是我的代码:

的jQuery

$("#searchValue").keyup(function() {

            var table = $(this).siblings('table').not(':hidden');
            $(table +" tr").each(function() {

            });   
});

HTML

<table id='tablePlanning' class='tablesorter'>
    <thead>
        <tr>
            <th>PR Code</th>
            <th>Klant</th>
            <th>Description</th>
            <th>Project status</th>
            <th>Project Leader</th>
            <th>Coordinator</th>
            <th>Account manager</th>
            <th>Billing</th>
            <th>Start Datum</th>
            <th>Hardware</th>
        </tr>
    </thead>
    <tbody>

    </tbody>
</table>

<table id='tableProject' class='tablesorter'>
    <thead>
        <tr>
            <th>Project ID</th>
            <th>Description</th>
            <th>Customer</th>
            <th>Status</th>
            <th>Max Hours</th>
            <th>Achieved</th>
            <th>Difference</th>
        </tr>
    </thead>
    <tbody>

    </tbody>
</table>

3 个答案:

答案 0 :(得分:4)

$(table +" tr")将获取table引用的jQuery对象,将其转换为字符串("[object Object]"),将" tr"附加到其中,然后尝试使用结果字符串"[object Object] tr"作为选择器。

您可能想要find,这是jQuery对象上可用的函数,用于在该jQuery对象的元素中搜索后代元素:

table.find('tr').each(/*...*/);

答案 1 :(得分:0)

您没有循环浏览可见表。

循环遍历可见表并在每个表内循环遍历行。

尝试:

$("#searchValue").keyup(function() {
    $("table:visible").each(function() {
        $(this).find("tr").each(function(){
            //do something
        });
    });   
});

答案 2 :(得分:0)

$(“#searchValue”)。keyup(function(){

  $('tr:visible','table.tablesorter:visible').each(function(){
     var trObj=$(this);
     //-- your code goes here
  });

});

使用的选择器说明: 选择所有可见表下的所有可见tr,其中类为“tablesorter”。 &安培; $(this)将引用每个tr对象。

相关问题