function search() {
$("#myInput").keyup(function() {
var value = this.value;
$("table").find("tr").each(function(index) {
if (index === 0) return;
var id = $(this).find("td").first().text();
$(this).toggle(id.indexOf(value) !== -1);
});
});
}
上面的代码是我用来在HTML中搜索我的表格,但是我无法想办法确保输入值,例如'hello'或'HeLLo',它在搜索时仍会显示。基本上我所说的是 - 我怎么做它所以这段代码不区分大小写?
同时使用小写或大写。
function search() {
$("#myInput").keyup(function() {
var value = this.value.toLowerCase();
$("table").find("tr").each(function(index) {
if (index === 0) return;
var id = $(this).find("td").first().text().toLowerCase();
$(this).toggle(id.indexOf(value) !== -1);
});
});
}
为什么不添加一个特殊的方法来搜索一个字符串是否包含不敏感的另一个字符串?:
代码开头的某个地方:
String.prototype.contains = function(src) {
var srcUpper = src.toUpperCase();
var thisUpper= this.toUpperCase();
return srcUpper.indexOf(thisUpper) > -1;
}
然后在任何您想要的地方使用它(您可以更改方法名称。
你的代码将变成这样:
function search() {
$("#myInput").keyup(function() {
var value = this.value;
$("table").find("tr").each(function(index) {
if (index === 0) return;
var id = $(this).find("td").first().text();
$(this).toggle(id.contains(value));
});
});
}