2017-04-26 8 views
0

こんにちは、テーブル内のjqueryを使用して検索する必要があります。しかし、問題は、私のコードは、すべての行の最初の要素だけを検索するのではなく、テーブル全体を検索して結果を表示することです。どうすればテーブル全体を検索できますか?ここでテーブルを使用したJquery検索

私のjqueryのコードここ

$("#search").on("keyup", function() { 
    var value = $(this).val(); 

    $(".table tr").each(function(index) { 
     if (index !== 0) { 

      $row = $(this); 

       var id = $row.find("td").text(); 

      if (id.indexOf(value) !== 0) { 
       $row.hide(); 
       </td>'); 
      } 
      else { 
       $row.show(); 

      } 
     } 


    }); 
}); 

は私のHTMLテーブルです:

<input type="text" class="form-control" name="search" id="search" placeholder="Search Records"> 
<table class="table table-bordered table-striped" id="employees"> 
         <thead> 
         <tr> 
          <th>No</th> 
          <th>Type</th> 
          <th>Name</th> 
          <th>Temp Address</th> 
          <th>Permanent Address</th> 
          <th>Mobile</th> 
          <th>Home</th> 
          <th>Update</th> 
         </tr> 
         </thead> 
         <tbody><tr> 

       <td>27006</td> 
       <td>Fixer</td> 
       <td>Sam</td> 
       <td>testing address</td> 
       <td></td> 
       <td>123456</td> 
       <td>123456</td> 
      </tr> 
      </tbody> 
      <tbody><tr> 

       <td>test</td> 
       <td>test</td> 
       <td>test</td> 
       <td>test</td> 
       <td>test</td> 
       <td>test</td> 
       <td>test</td> 
      </tr> 
      </tbody> 
      </table> 
+0

ちょっと、あなたのhtmlの$( "#search")idはどこですか?コードスニペットできる場合は、素晴らしいでしょう。 – Harsheet

+0

Abbasi

+0

なぜホイールを改造するのですか? https://datatables.net/を使用する – funcoding

答えて

3

はこれを試してみてください -

$("#search").keyup(function(){ 
 
     _this = this; 
 
     $.each($("#employees tbody tr"), function() {   
 
     if($(this).text().toLowerCase().indexOf($(_this).val().toLowerCase()) === -1) 
 
       $(this).hide(); 
 
      else 
 
       $(this).show();     
 
     }); 
 
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type="text" class="form-control" name="search" id="search" placeholder="Search Records"> 
 
<table class="table table-bordered table-striped" id="employees"> 
 
        <thead> 
 
         <tr> 
 
          <th>No</th> 
 
          <th>Type</th> 
 
          <th>Name</th> 
 
          <th>Temp Address</th> 
 
          <th>Permanent Address</th> 
 
          <th>Mobile</th> 
 
          <th>Home</th> 
 
          <th>Update</th> 
 
         </tr> 
 
         </thead> 
 
         <tbody> 
 
         <tr> 
 
         <td>27006</td> 
 
         <td>Fixer</td> 
 
         <td>Sam</td> 
 
         <td>testing address</td> 
 
         <td></td> 
 
         <td>123456</td> 
 
         <td>123456</td> 
 
         </tr> 
 
       </tbody> 
 
      <tbody><tr> 
 

 
       <td>test</td> 
 
       <td>test</td> 
 
       <td>test</td> 
 
       <td>test</td> 
 
       <td>test</td> 
 
       <td>test</td> 
 
       <td>test</td> 
 
      </tr> 
 
      </tbody> 
 
      </table>

ありがとう

関連する問題