任何幫助將不勝感激!根據參數字符串值顯示JSON值
這是我迄今爲止所做的。
我有一個index.html頁面和一個notice-details.html頁面。在index.html頁面上,我有一個數據網格,並且顯示來自json文件的'title'。我將標題超鏈接到details.html,並添加了「數字」,因此每個標題href都是唯一的。在數據網格中的鏈接標題的數據模板看起來像這樣:
<a href="notice-details.html?id={{number}}">{{title}}</a>
在通知-details.html頁我試圖捕捉查詢字符串參數,並顯示匹配的相關鍵值對'數字'在json數組中。因此,如果我在notice-details.html?id = 2012-01上登陸,我希望在該頁面上顯示與json中與2012-001相關的標題,獎勵索賠到期日,獎勵索賠表和日期。
我被困在如何匹配數字和只查詢匹配的內容。
JSON:
{
"notices": [
{
"number": "2012-001",
"link": "google.com",
"title": "sample title",
"awardClaimDueDate": "",
"awardClaimForms": "",
"datePosted": "1/31/2012"
},
{
"number": "2012-001",
"link": "google.com",
"title": "sample title",
"awardClaimDueDate": "",
"awardClaimForms": "",
"datePosted": "1/31/2012"
}
]
}
JS:
function jsonParser(json){
$('#load').fadeOut();
$.getJSON('notices.json',function(data){
// Parse ID param from url
var noticeParamID = getParameterByName('id');
$.each(data.notices, function(k,v){
var noticeNumber = v.number,
noticeTitle = v.title,
claimDueDate = v.awardClaimDueDate,
claimForms = v.awardClaimForms,
date = v.datePosted;
if(noticeParamID == noticeNumber){
// how can I display content that matches the url param value (noticeURLNumber)?
}
});
});
}
// get URL parameter by name
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
你能調整你的JSON或添加其他JSON是結構更好的爲您查找使用情況?另外,通知號碼是否不唯一? –