2014-11-21 47 views
0

我有我的頁面上的表,它上面的過濾文本框的作品太棒了,使用下面的JQuery:復位按鈕

 $("#searchInputCompanies").keyup(function() { 
      //split the current value of searchInput 
      var data = this.value.split(" "); 
      //create a jquery object of the rows 
      var jo = $("#cBody").find("tr"); 
      if (this.value == "") { 
       jo.show(); 
       return; 
      } 
      //hide all the rows 
      jo.hide(); 

      //Recusively filter the jquery object to get results. 
      jo.filter(function(i, v) { 
       var $t = $(this); 
       for (var d = 0; d < data.length; ++d) { 
        if ($t.text().toLowerCase().indexOf(data[d].toLowerCase()) > -1) { 
         return true; 
        } 
       } 
       return false; 
      }) 
      //show the rows that match. 
      .show(); 

      $('#selectAllCompanies').prop('checked', ''); 
     }).focus(function() { 
      this.value = ""; 
      $(this).css({ 
       "color": "black" 
      }); 
      $(this).unbind('focus'); 
     }).css({ 
      "color": "#C0C0C0" 
     }); 

我怎樣才能建立一個復位過濾按鈕爲了這?

回答

1

呃,這是一個相當糟糕的執行:( 首先,您需要更改事件$("#searchInputCompanies"),使這一切更容易一點。因此,它將成爲$("#searchInputCompanies").on("input", function() {...

$("#resetAction").on("whatEventYouWant", function() { 
    $("#searchInputCompanies").val("").trigger("input"); 
}); 

這將觸發input事件$("#searchInputCompanies"),並且由於文本框爲空,所有行都將變爲可見。

+0

'$('#companyReset')。on(「click」,function(){$(「#searchInputCompanies」).val('' );});'確實清除了過濾器框,但它不會觸發過濾器運行(並因此清除) – PKD 2014-11-21 22:42:33

+1

嗯,這很奇怪,但沒有問題:'$(「#resetAction」)。on(「whatEventYouWant」,function(){0} ); //或'keyup'如果你沒有改變事件 });'編輯主要答案。 – kappaallday 2014-11-21 22:43:59

+0

輝煌。謝謝。 – PKD 2014-11-21 22:45:21