我正在尝试编写一个可以使用逻辑运算符过滤结果的搜索功能。代码的工作方式是我可以输入要搜索的字符串,或者我可以将 < 或 > 放在文本字段的开头以过滤更大或更小的数值。我正在寻找更好的版本或编写方式。我有一个(半)工作版本,但我写它的方式如果值不超过三位数,我必须放一个或两个零。例如,<050 必须用于搜索 50 岁以下的项目。单独使用 <50 会导致错误的结果。此外,如果结果低于 100,我必须将值从 5 转换为 005。这仅在最大值低于 1k 时才有效,否则我必须转换为 0005。
function FullListSearch() {
$("#InfoSearch").keyup(function() {
var V = this.value;
var value = this.value.toLowerCase();
Operator = value[0];
len = $(this).val().length;
if ( Operator == ">" ) {
$("#Info").find(".fulllist").each(function() {
var ths = $(this).text();
var Npattern = /[0-9]+/g;
var Nmatches = ths.match(Npattern);
var ValN = V.match(Npattern);
if (Nmatches.length > "1") {
if (Nmatches[1].length == "1") { Nmatches[1] = "00" + Nmatches[1] };
$(this).show();
if (Nmatches[1] < ValN) {
$(this).toggle();
}
}
else { alert("the > operator is not valid with this object");
return false;
}
});
}
else if ( Operator == "<" ) {
$("#Info").find(".fulllist").each(function() {
var ths = $(this).text();
var Npattern = /[0-9]+/g;
var Nmatches = ths.match(Npattern);
var ValN = V.match(Npattern);
if (Nmatches.length > "1") {
if (Nmatches[1].length == "1") { Nmatches[1] = "00" + Nmatches[1] };
$(this).show();
if (Nmatches[1] > ValN) {
$(this).toggle();
}
}
else { alert("the > operator is not valid with this object");
return false;
}
});
}
else {
$("#Info").find(".fulllist").each(function() {
var id = $(this).first().text().toLowerCase()
$(this).toggle(id.indexOf(value) !== -1);
});
}
});
}