这是我的HTML。
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"> </script>
<script>
$(function() {
var people = [];
$.getJSON('test.json', function(data) {
$.each(data.person, function(i, f) {
var tblRow = "<tr>" + "<td>" + f.ID + "</td>" +"<td>" + f.Destination + "</td>" +
"<td>" + f.SupplierName + "</td>" + "</tr>"
$(tblRow).appendTo("#userdata tbody");
});
});
});
</script>
</head>
<body>
<table id= "userdata" border="2">
<thead>
<th>ID</th>
<th>Destination</th>
<th>SupplierName</th>
</thead>
<tbody>
</tbody>
</table>
</body>
</html>
这是我的test.json文件
{
"person" : [
{
"ID" : 100,
"Destination" : "brisbane",
"SupplierName" : "Hilton Brisbane",
},
{
"ID" : 101,
"Destination" : "brisbane",
"SupplierName" : "Hotel Grand Chancellor Brisbane",
},
{
"ID" : 102,
"Destination" : "brisbane",
"SupplierName" : "Park Regis North Quay",
}
]
}
我正在尝试做什么
输出正常。但这就是我想要做的事情。
我的网站看起来像www.website.com?deal=100
我想在'deal'参数的末尾查找ID,并仅从JSON数组中输出该ID的信息。 JSON数组有一个具有相同ID的对象。你能帮忙吗?
答案 0 :(得分:1)
执行if以测试id是否与url
中的id相对应 $.getJSON('test.json', function(data) {
$.each(data.person, function(i, f) {
if(window.location.href.split('=')[1] == f.ID) {//asumes that you have only the deal as a url param
var tblRow = "<tr>" + "<td>" + f.ID + "</td>" +"<td>" + f.Destination + "</td>" +
"<td>" + f.SupplierName + "</td>" + "</tr>"
$(tblRow).appendTo("#userdata tbody");
}
});
答案 1 :(得分:1)
试试这个:
$.each(data.person, function(i, f) {
var param = window.location.href.slice(window.location.href.indexOf('?') +
1).split('=');
var id = param[1];
if (f.id == id){
var tblRow = "<tr>" + "<td>" + f.ID + "</td>" +"<td>" + f.Destination + "
</td>" + "<td>" + f.SupplierName + "</td>" + "</tr>"
$(tblRow).appendTo("#userdata tbody");
}
});