2017-02-09 124 views
0

我有一個表格和一個搜索文本框。我過濾表時,我使用這個腳本在搜索框輸入內容:搜索結果時隱藏表格0

$(document).ready(function(){ 
    // Write on keyup event of keyword input element 
    $("#buscador").keyup(function(){ 

     var $rows1 = $('#tablaproyectos1 tbody>tr'); 
     $('#buscador').keyup(function() { 
      var val = $.trim($(this).val()).replace(/ +/g, ' ').toLowerCase(); 

      $rows1.show().filter(function() { 
       var text = $(this).text().replace(/\s+/g, ' ').toLowerCase(); 
       return !~text.indexOf(val); 
      }).hide(); 
     }); 
    }); 
}); 

我的問題是:我如何隱藏表時,有沒有從搜索結果?

回答

0

像這樣的東西應該工作:

$(document).ready(function(){ 
 
    // Write on keyup event of keyword input element 
 
    $("#buscador").keyup(function(){ 
 

 
     var $rows1 = $('#tablaproyectos1 tbody>tr'); 
 
     $('#buscador').keyup(function() { 
 
      $('#tablaproyectos1').show(); 
 
      var val = $.trim($(this).val()).replace(/ +/g, ' ').toLowerCase(); 
 

 
      $rows1.show().filter(function() { 
 
       var text = $(this).text().replace(/\s+/g, ' ').toLowerCase(); 
 
       return !~text.indexOf(val); 
 
      }).hide().addClass('hide'); 
 
      if ($('#tablaproyectos1 tbody>tr:visible').length == 0) { 
 
       $('#tablaproyectos1').hide(); 
 
      } 
 
     }); 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type="text" id="buscador" /> 
 
<table id="tablaproyectos1" style="border: 2px solid blue;"> 
 
    <tbody> 
 
    <tr> 
 
     <td>Esto es una prueba de celda</td> 
 
     </tr> 
 
    <tr> 
 
     <td>Texto de relleno</td> 
 
     </tr> 
 
    <tr> 
 
     <td>Otra celda de prueba</td> 
 
     </tr> 
 
    <tr> 
 
     <td>Suficiente para comprobar el buscador</td> 
 
     </tr> 
 
    </tbody> 
 
    </table>

它的工作原理計數可見行。如果這個總長度爲0,則隱藏父表。表格始終顯示在開頭。

+0

感謝您的回答。我有點麻煩。在你的代碼片段中它是完美的,但是當我把代碼放在我的頁面中時,它隱藏了表格,但是如果我刪除了文本字段上的文本,表格就不會再顯示出來。我能做什麼? –

+0

修復了它。我剛剛刪除了.addClass('hide') –

+0

不客氣。是的,我嘗試先按課程隱藏表格,然後忘記刪除此代碼。 –

相關問題