論理演算子を使用して結果をフィルタリングできる検索関数を作成しようとしています。コードが機能する方法は、検索する文字列を入力するか、テキスト フィールドの先頭に < または > を配置して、数値の増減をフィルター処理することです。より良いバージョンまたはそれを書く方法を探しています。私は (半) 動作するバージョンを持っていますが、私が書いた方法では、値が 3 桁を超えない場合、1 つまたは 2 つのゼロを入力する必要があります。たとえば、50 未満のアイテムを検索するには、<050 を使用する必要があります。<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);
});
}
});
}