0

我正在尝试编写一个可以使用逻辑运算符过滤结果的搜索功能。代码的工作方式是我可以输入要搜索的字符串,或者我可以将 < 或 > 放在文本字段的开头以过滤更大或更小的数值。我正在寻找更好的版本或编写方式。我有一个(半)工作版本,但我写它的方式如果值不超过三位数,我必须放一个或两个零。例如,<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);
    });
    }
  });
}
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

这是我的工作解决方案。感谢 EZSlaver 建议我将我的 int 存储在一个属性中,并建议我不使用正则表达式进行验证。

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 回答