我正在迭代我的JSON响应。我得到了参数名为id的HTML元素。
例如,我的JSON响应包含一个"costcenter":"1234"
,并且有一个id为costcenter
的<span>
元素。
现在,我认为可以遍历JSON数组并自动读取它的名称,而不是为每个id编写一条语句。
这就是我得到的
$(".dataset").click(function() {
changeid = this.id;
$.ajax({
url: "source",
dataType: "json",
contentType: "application/json; charset=utf-8",
data: {
id: changeid
},
success: function(data) {
// How to get the name of the parameter, and then read it's value?
}
})
})
JSON看起来是这样的,它只有一个维度和一个结果集:{"changeid":"1","costcenter":"478","manager":"John Smith"}
发布于 2012-06-27 18:24:30
如果我理解你的问题,你希望在你的成功函数中有这样的东西:
for (var key in data) {
$('#'+key).html(data[key]);
}
https://stackoverflow.com/questions/11232621
复制