使用jQUERY$.each()迭代返回的JSON对象
我使用jQuery的$.ajax方法调用getjson.php,它使用PHPs JSON_encode($data)返回一个JSON对象。我的JSON的结构如下所示 [ { “StoreKey”:“84”, “店名”:“区域1”, "0": "4,055.37", "1": "2,668.29", "2": "4,454.81", "3": "4,789.99", “4”:“无”, “5”:“无”, “6”:“无”, "7": "15,968.46" }, { “StoreKey”:“26”, “店名”:“区域2”, "0": "2,368.09", "1": "2,270.24", "2": "1,806.76", "3": "1,656.15", “4”:“无”, “5”:“无”, “6”:“无”, "7": "8,101.24" }, { “StoreKey”:“每日”, “店名”:“总计”, "0": "92,614.45", "1": "98,126.78", "2": "104,157.04", "3": "102,581.87", “4”:“无”, “5”:“无”, “6”:“无”, "7": 397480.14 }] 我可以使用$('#responseDiv').html(result);})显示JSON对象; 但是我想使用$.each()方法分析每一行 使用$.each()遍历JSON对象时,只显示最后一个JSON对象。这将显示最后一个JSON对象->“7”:397480.14使用jQUERY$.each()迭代返回的JSON对象,jquery,json,iteration,Jquery,Json,Iteration,我使用jQuery的$.ajax方法调用getjson.php,它使用PHPs JSON_encode($data)返回一个JSON对象。我的JSON的结构如下所示 [ { “StoreKey”:“84”, “店名”:“区域1”, "0": "4,055.37", "1": "2,668.29", "2": "4,454.81", "3": "4,789.99", “4”:“无”, “5”:“无”, “6”:“无”, "7": "15,968.46" }, { “StoreKey”:“26”,
var data = $.parseJSON(result);
$.each(data,function(row,store) {
$.each(store,function(key,value) {
$('#responseDiv').html(value);
});
})
我们的目标是将JSON对象包装为每行的$.ajax //jQuery Syntax-ajax.api!
({
type: "POST",
url: "includes/getjson.php", //----my php scripts/codes
data: "date="+x,
datatype: "json",
success: function(result)
{
var data = $.parseJSON(result);
$.each(data,function(row,store) {
$.each(store,function(key,value) {
$('#responseDiv').html(value); });
})
}
});
}
这是我没有做或做得不正确的事情…您每次都在替换#responseDiv的内容-您想附加它吗
$('#responseDiv').html($('#responseDiv').html() + value);
或简称:
$('#responseDiv').append(value);
您每次都要替换#responseDiv的内容-您想附加它吗
$('#responseDiv').html($('#responseDiv').html() + value);
或简称:
$('#responseDiv').append(value);
如果您试图创建一个不基于json对象的行的表,我建议使用像or这样的模板引擎。然后,您只需创建一个模板,并将返回对象发送给它进行渲染
<!-- Template content -->
<textarea id="myTemplate" style="display:none">
{#foreach $T as record}
<tr>
<td>{$T.record.StoreKey}</td>
<td>{$T.record.StoreName}</td>
<td>{$T.record.0}</td>
</tr>
{#/for}
</textarea>
如果您试图创建一个不基于json对象的行的表,我建议使用像or这样的模板引擎。然后,您只需创建一个模板,并将返回对象发送给它进行渲染
<!-- Template content -->
<textarea id="myTemplate" style="display:none">
{#foreach $T as record}
<tr>
<td>{$T.record.StoreKey}</td>
<td>{$T.record.StoreName}</td>
<td>{$T.record.0}</td>
</tr>
{#/for}
</textarea>