2017-02-09 85 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

不客气。是的,我尝试先按课程隐藏表格,然后忘记删除此代码。 –

相关问题