使用URL过滤JQuery列表

时间:2011-07-12 13:22:50

标签: javascript jquery filter

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

E.g。

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

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

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

所以我想说我的网址看起来像这样:

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 个答案:

答案 0 :(得分:2)

如果在?之后总是只有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/