2012-09-28 44 views
0

你好我正在使用這個js來傳遞url參數,它工作得很好,但我的問題是,當我定義JSON文件的路徑時,我不想使用項目的ID ...我想使用另一個ID。例如:我有以下項目:將參數傳遞到另一個頁面以從Json生成listview

{"id":"1", 
"name":"Winery", 
"street":"Chile", 
"number":"898", 
"phone":"4204040", 
"mail":"[email protected]", 
"web":"www.winery.com", 
"lat":"-32.891638", 
"long":"-68.846522", 
"id_localidad":"1", 
"id_provincia":"1"} 

我想把id_localidad在路徑的末尾,以生成取決於城市列表視圖(id_localidad是城市裏的店鋪的ID),而不是項目的ID。這不適合我。

在此先感謝!

JS文件

$('#PuntosDeVenta').live('pageshow',function(event){ 


var id = getUrlVars()["id"]; 


$.getJSON('http://localhost/CavaOnline/json_PuntosDeVentas.php?id_localidad='+id, function(vinerias) { 
//THIS IS NOT WORKING, IS THE SAME AS PUTTING id, not id_localidad 

$.each(vinerias, function(index, vineria) { 

    $('#listviewVinerias').append('<li><a href="FichaTecnicaVineria.php?id=' + vineria[id - 1].id + '" > ' + 
       '<img src="pics/' + vineria[id - 1].img_url1 + '"/>' + 
       '<h4>' + vineria[id - 1].name+'</h4>' + 
       '<p>' + vineria[id - 1].street+ ' ' + vineria[id - 1].number+ '</p>' + 
       '</a></li>'); 

    $('#listviewVinerias').listview('refresh') 

    }); 

}); 

}); 

function getUrlVars() { 

var vars = [], hash; 

var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&'); 

for(var i = 0; i < hashes.length; i++) 

{ 

    hash = hashes[i].split('='); 

    vars.push(hash[0]); 

    vars[hash[0]] = hash[1]; 

} 

return vars; 

} 

事業部,我加載列表

<div data-role="content"> 

<ul id="listviewVinerias" data-role="listview"></ul> 

</div> 

回答

0

所以我假設你vinerias是包含JSON對象列表的變量,即使我不知道你爲什麼到處打電話給[id-1]。 如果是這樣,您可以使用.filter()函數過濾出id_localidad等於指定值的元素。

var filteredVinerias = vinerias.filter(function(index){ 
    return this["id_localidad"] === "1" //The localidad you want 
}); 
相關問題