0

論理演算子を使用して結果をフィルタリングできる検索関数を作成しようとしています。コードが機能する方法は、検索する文字列を入力するか、テキスト フィールドの先頭に < または > を配置して、数値の増減をフィルター処理することです。より良いバージョンまたはそれを書く方法を探しています。私は (半) 動作するバージョンを持っていますが、私が書いた方法では、値が 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);
    });
    }
  });
}
4

2 に答える 2

1

私は次のようなものを使用します:

function FullListSearch() {
    $("#InfoSearch").keyup(function () {
        var V = this.value;
        var value = this.value.toLowerCase();
        var Operator = value[0];

        var compFunc = function (a, b) {
            return a == b;
        };

        if (Operator == '>' || Operator == '<') {
            value = value.substring(1);

            if (Operator == '>') compFunc = function (a, b) {
                return a > b;
            };
            else // '<' 
            compFunc = function (a, b) {
                return a < b;
            };
        }
        var numVal = parseInt(value);
        if (isNaN(numVal)) alert("invalid input!");

        $("#Info").find(".fulllist").each(function () {
            var val = parseInt($(this).text());
            $(this)[compFunc(val, numVal) ? "show" : "hide"]();
        });
    });
}

私はこれをチェックしていませんが、アイデアは明確なはずです。

いくつかのメモ:

  1. 正規表現は必ずしも検証に最適な方法ではありません。その場合/^[><]?\d+$/は、全体を検証するなど、より一般的な方法を使用してください。

  2. 数値を比較できるのに、なぜ各桁を比較するのですか? を使用しparseInt()ます。

  3. キーアップごとにこのコード全体を起動するわけではありませんが、新しいキーが押されたかどうかを確認するためにしばらく待ちます。何かのようなもの:

     $("#InfoSearch").keyup(function () {
          var timeout = $(this).data("timeout");
          if (timeout) clearTimeout(timeout);
          $(this).data("timeout", setTimeout(function () {...........}, 200); // milliseconds
     }
    
于 2013-08-13T22:36:32.833 に答える
0

これが私の実用的なソリューションです。int を属性内に格納することを提案し、検証に正規表現を使用しないことを提案してくれた EZSlaver に感謝します。

function FullListSearch() {
  $("#InfoSearch").keyup(function() {
  var V = parseInt(this.value);
      var value = this.value.toLowerCase();
  Operator = $('select option:selected').val();

   $("#Info").find(".fulllist").each(function() {
     $(this).show();
     var NumVal = $(this).attr('time');
     if ( (Operator == 'greater') && (NumVal < V)) {
      $(this).hide();
     }
     if ( (Operator == 'lesser') && (NumVal > V)) {
      $(this).hide();
    }
     if (Operator == "equal") {
       var id = $(this).first().text().toLowerCase()
       $(this).toggle(id.indexOf(value) !== -1);
     }
   });
  });   
}
于 2013-08-14T17:28:25.690 に答える