【问题标题】:check Text is belongs to which categoty in a list in jquery?检查文本属于jquery列表中的哪个类别?
【发布时间】:2017-06-16 23:14:45
【问题描述】:

我正在使用一个类别数组,即CategoryListSearch,其中包含各种类别,如移动电子产品、珠宝等。

我正在使用文本框 在该文本框中,我正在插入一个值,例如 iPhone,所以我如何从列表中匹配我的手机属于移动类别。 我用这个作为样本

if (txt != '') {
  $.each(CategoryListSearch, function(i, o) {
});

但不知道如何比较来自CategoryListSearch 的文本值。

请帮忙!

【问题讨论】:

  • 拥有近 300 个代表,您应该知道如何单击 <> 并创建 minimal reproducible example - 什么是 CategoryListSearch?
  • CategoryListSearch 是数组还是对象?我们需要这个神秘变量的内容示例。
  • 你能给我们看看 CategoryListSearch 的 sn-p 吗?

标签: javascript jquery asp.net list search


【解决方案1】:

试试这个

if(CategoryListSearch.indexOf(text) > -1) {
  //text is present
}

【讨论】:

    【解决方案2】:

    像这样。在循环中使用三元运算符(?:)

    if (txt != '') {
      $.each(CategoryListSearch, function(i, o) {
          var present = (o == txt) ? "Item present in list" : "No item in list";
          console.log(present);
        }
      });
    

    【讨论】:

      【解决方案3】:

      假设您的对象是树结构的,以下函数首先检查我们是否正在搜索对象本身。如果不是,则对其进行迭代并搜索索引/成员。

      getCategoryPath(obj, item) {
          if (obj === item) {
              return obj;
          }
          for (index in obj) {
              if ((typeof obj[index] === "undefined") || (obj[index] === null)) {
                  //irrelevant leaf
              } else if (typeof obj[index] === "object") {
                  var ret = getCategoryPath(obj[index], item);
                  if (ret) {
                      return ret.unshift(index);
                  }
              } else if ((obj[index] === item) || (index === item)) {
                  return [index];
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-11-15
        • 1970-01-01
        • 2016-05-28
        • 2013-02-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-10
        相关资源
        最近更新 更多