2014-01-23 80 views
0

我终于得到这个工作,我可以通过名称搜索过滤。但是,我不能通过在任何列中搜索值来过滤它。例如,我希望能够键入'gmail'并仅显示那行中任何表单元格中具有gmail的记录。即使我将class="name"添加到每个td,它们都不包含在搜索中。jQuery正则表搜索没有在所有列中搜索,只有第一列

HTML:

<input type="search" id="search" placeholder="Search table by name"> 

    <table caption="Data type 1" sortable id="name_table"><col width="20%"><col width="20%"><col width="15%"><col width="20%"><col width="25%"> 
     <thead> 
      <tr> 
       <th>Name</th><th>Address</th><th>Job No.</th><th class="no-sort">Phone</th><th>Email</th> 
      </tr> 
     </thead> 
     <tfoot> 
      <tr> 
       <td colspan="5">End of records</td> 
      </tr> 
     </tfoot> 
     <tbody> 
      <tr> 
       <td class="name">John Smith</td><td>123 Fake Street</td><td>Job 117</td><td>0405788996</td><td>[email protected]</td> 
      </tr> 
      <tr> 
       <td class="name">Jane Smyth</td><td>1234 False Road</td><td>Job 118</td><td>0405788555</td><td>[email protected]</td> 
      </tr> 
      <tr> 
       <td class="name">Alexey Fadeev</td><td>12 Dubio Lane</td><td>Job 119</td><td>0405788966</td><td>[email protected]</td> 
      </tr> 

     </tbody> 
    </table> 

的Javascript:

<script> 
    // Table Search 
    $('#search').keyup(function() { 
     var regex = new RegExp($('#search').val(), "i"); 
     var rows = $('table tbody tr'); 
     rows.each(function (index) { 
     name = $(this).children(".name").html() 
     if (name.search(regex) != -1) { 
      $(this).show(); 
     } else { 
      $(this).hide(); 
     } 
     }); 
    }); 
</script> 

我很想能够内<tbody>我想在搜索中包括类添加到每列或单元格。有小费吗?

回答

0

使用.html()方法时,它将返回堆栈中第一个元素的HTML。我可以建议你改变这一行:

name = $(this).children(".name").html(); 

这样:

name = $(this).text(); 
+0

啊。这就说得通了。现在我想知道我是如何错过它的。 – ladam24

1

你是不是包括在TR使用的所有文字

$(this).html(); or $(this).text(); 

,而不是

name = $(this).children(".name").html(); 
+0

谢谢你的回答。 – ladam24

相关问题