jquery implements table search function

Original address: http://www.cnblogs.com/bigtallhcy/p/5344924.html

In the operation of the table, it is often encountered to search for results by keywords. This function is very simple to implement with the filter of jquery.
I illustrate with a small example:
<table>

   <thead>
   <tr colspan="3">
      <input type="text" id="filtertxt">
      <input type="button" id="ss" value="搜索" style="margin-left: 50px;"/>
   </tr>
      <tr><th>姓名</th><th>性别</th><th>暂住地</th></tr>
   </thead>
   <tbody>
      <tr><td>张山</td><td>男</td><td>浙江宁波</td></tr>
      <tr><td>李四</td><td>女</td><td>浙江杭州</td></tr>
      <tr><td>王五</td><td>男</td><td>湖南长沙</td></tr>
      <tr><td>找六</td><td>男</td><td>Zhejiang Wenzhou</td></tr> 
      <tr><td>Li Zi</td><td>Female</td><td>Zhejiang Hangzhou</td></tr><tr><td>Li Zi</td><td>Female</td><td>Zhejiang Hangzhou</td></tr><tr><td>Li Zi</td><td>Female</td><td>Zhejiang Hangzhou</td></tr>      <tr><td>Wang Liu</td><td>Male</td> <td>Zhejiang Hangzhou</td></tr>      <tr ><td>MAXMAN</td><td>Female</td><td>Zhejiang Hangzhou</td></tr>      <tr><td>Rain</td><td>male</td><td>Zhejiang Hangzhou</td></tr>



      <tr><td>李四</td><td>男</td><td>湖南长沙</td></tr>
   </tbody>
</table>
 The result is a form plus a simple search box, search button.
When the search is clicked, use jquery to implement the query:
<script type="text/javascript">
  $(function(){
     $('#ss').click(function(){
        var sstxt=$('#filtertxt').val();
        $("table tbody tr")
              .hide()
              .filter(":contains('"+sstxt+"')")
              .show();
     })

  })
</script>
 
The basic idea:

 

Hide all the information first, then query and filter out the results, and then display the results.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326543402&siteId=291194637