javascript - 使用 URL 过滤 JQuery 列表

标签 javascript jquery filter

我需要修改一些已经存在的代码。有一段代码使用 URL 来过滤 JQuery 列表以填充搜索输入。

例如

http://***/store/mobile/page/productList.page?search=football

自动在搜索栏中输入“足球”。

现在我需要过滤列表,而不使用搜索栏。

假设我的 URL 看起来像这样:

http://***/store/mobile/page/productList.page?football

这将在不使用搜索栏的情况下过滤足球列表。

这是我需要更改的代码。如果我的问题不清楚,请告诉我。

$('div[data-url*="productList"]').live("pageshow", function() {

  filterValue = getParameterByName("search", location.search);

  if (filterValue) {

    $('input[data-type="search"]').val(filterValue);
  }

  refreshList();
});

和:

 $.each(catalog.products,
      function(index, value) {

          if ((!filterValue )
                  || value.name.toUpperCase().indexOf(filterValue.toUpperCase()) != -1
                  || value.brand.toUpperCase().indexOf(filterValue.toUpperCase()) != -1)

          {
              items.push('<li id="' + index + '">' +
                      '<a data-identity="productId"  href="./details.page?productId=' + index + '" >' +
                      '<img class="ui-li-thumb" src="' + value.thumbnail + '"/>' +
                      '<p>' + value.brand + '</p>' +
                      '<h3>' + value.name + '</h3>' +
                      '<span class="ui-li-count">' + value.price + ' $</span></li>') +
              '</a>';
          }

      });

最佳答案

如果 ? 之后总是只有 1 个参数,那么您可以简单地从 javascript 中的页面位置获取它,例如

var url = document.location;
var params = url.split("?");

filterValue = params[params.length-1]
if (filterValue) {

    $('input[data-type="search"]').val(filterValue);
}

refreshList();

示例:http://jsfiddle.net/yPgPc/

关于javascript - 使用 URL 过滤 JQuery 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6664844/

相关文章:

javascript - JQuery val 为 null 或不是对象

javascript - 为什么只显示最后一项而不是全部

javascript - Firefox 在 for 循环中抛出 js 错误 "allocation size overflow"

javascript - 'this' 不是指正确的方法

javascript - 从父函数中检索函数参数

jquery - 获取 jQuery .filter() 结果的相反/相反结果

python - scipy.signal.wiener 总是显示警告

javascript - 动态输入文本框未绑定(bind)到自动完成

jquery - debug : jquery fails silently, 如何防止它

javascript - 使用复选框过滤多个类