我需要修改一些已经存在的代码。有一段代码使用 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();
关于javascript - 使用 URL 过滤 JQuery 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6664844/