2011-07-12 63 views
0

我需要修改一些已經存在的代碼。有一段代碼可以使用URL過濾JQuery列表來填充搜索輸入。用URL過濾JQuery列表

E.g.

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>'; 
      } 

     }); 
+0

你可能要剪掉領先from window.location.search –

回答

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/

+1

這是一個很好的方法,如果以後需要改變它,只需要改變'filterValue'的值。是否應該用'var'聲明來防止它成爲全局? – StuperUser

+0

感謝您的幫助。試着讓它現在工作,我會保持你的發佈。 – JFFF

+0

我很抱歉問,但我似乎無法完成這項工作。我非常感謝幫助,但如果你可以更精確,那就太棒了! – JFFF