[英]Javascript Case Sensitive filter
我正在使用下面的腳本來過濾表中的結果。 唯一的問題是它區分大小寫。 我將如何使其不區分大小寫?
<script>
$(document).ready(function() {
$("#searchInput").keyup(function(){
//hide all the rows
$("#fbody").find("tr").hide();
//split the current value of searchInput
var data = this.value.split(" ");
//create a jquery object of the rows
var jo = $("#fbody").find("tr");
//Recusively filter the jquery object to get results.
$.each(data, function(i, v){
jo = jo.filter("*:contains('"+v+"')");
});
//show the rows that match.
jo.show();
//Removes the placeholder text
}).focus(function(){
this.value="";
$(this).css({"color":"black"});
$(this).unbind('focus');
}).css({"color":"#C0C0C0"});
});
</script>
http://jsfiddle.net/baeXs/和http://css-tricks.com/snippets/jquery/make-jquery-contains-case-insensitive/將幫助您
$.expr[":"].containsNoCase = $.expr.createPseudo(function(arg) {
return function( elem ) {
return $(elem).text().toUpperCase().indexOf(arg.toUpperCase()) >= 0;
};
});
可以用filter()
做這樣的事情:
$.each(data, function (i, v) {
v = v.toLowerCase();
jo.filter(function () {
var txt = $(this).text().toLowerCase();
return txt.indexOf(v) > -1;
}).show();
})
$(document).ready(function() {
$("#searchInput").keyup(function(){
//hide all the rows
$("#fbody").find("tr").hide();
//split the current value of searchInput
var data = this.value.toLowerCase().split(" ");
//create a jquery object of the rows
var jo = $("#fbody").find("tr");
//Recusively filter the jquery object to get results.
$.each(data, function(i, v){
jo = jo.filter("*:contains('"+v.toLowerCase()+"')");
});
//show the rows that match.
jo.show();
//Removes the placeholder text
}).focus(function(){
this.value="";
$(this).css({"color":"black"});
$(this).unbind('focus');
}).css({"color":"#C0C0C0"});
});
我可能會更改過濾器:
$.each(data, function(i, v) {
jo = jo.filter(function() {
return $(this).text().toLowerCase().indexOf(v.toLowerCase()) > -1;
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.