Twitter Bootstrap行过滤器/搜索框

use*_*129 14 html search filter twitter-bootstrap

我很难找到关于如何为Twitter Bootstrap创建简单搜索查询或行过滤器的教程.我尝试了很多,我不确定我做错了什么或者插件与Bootstrap不兼容.如果可以的话请帮忙.

我试过了:

$(document).ready(function() {
 //Declare the custom selector 'containsIgnoreCase'.
      $.expr[':'].containsIgnoreCase = function(n,i,m){
          return jQuery(n).text().toUpperCase().indexOf(m[3].toUpperCase())>=0;
      };

      $("#search").keyup(function(){

          $("#tabela").find("tr").hide();
          var data = this.value.split(" ");
          var jo = $("#tabela").find("tr");
          $.each(data, function(i, v){

               //Use the new containsIgnoreCase function instead
               jo = jo.filter("*:containsIgnoreCase('"+v+"')");
          });

          jo.show();

      }).focus(function(){
          this.value="";
          $(this).css({"color":"black"});
          $(this).unbind('focus');
      }).css({"color":"#C0C0C0"});
});
Run Code Online (Sandbox Code Playgroud)

没有这个...也许我在我的桌子或搜索框上缺少任何"id",我对此很新.

小智 46

这是我使用的:

$('input.filter').live('keyup', function() {
    var rex = new RegExp($(this).val(), 'i');
    $('.searchable tr').hide();
        $('.searchable tr').filter(function() {
            return rex.test($(this).text());
        }).show();
    });
Run Code Online (Sandbox Code Playgroud)

要使用它,你只需要创建一个表,其中tbody为"searchable"类,然后在页面的某个位置输入类"filter"(我更喜欢将它们放在搜索图标后面的Bootstrap Popup中).

  • 使用jQuery> 1.7` .live()`应该是`.on()`. (10认同)
  • 先生.我不知道你是否还在,但上面的代码让我感到沮丧.谢谢!! (5认同)

Gio*_*TBS 21

这是Filipp Lepalaan提供的解决方案的实例.非常感谢这个小而完美的代码.

$(document).ready(function () {

(function ($) {

    $('#filter').keyup(function () {

        var rex = new RegExp($(this).val(), 'i');
        $('.searchable tr').hide();
        $('.searchable tr').filter(function () {
            return rex.test($(this).text());
        }).show();

    })

}(jQuery));
Run Code Online (Sandbox Code Playgroud)

});