我是新手,并学会将我的旧式编码转移到这些。 现在我需要帮助。
我的JSON(数组) - 来自我的php json_encode
的结果:
{"e_id":"12101","e_password":kkkk,"e_secretQuestion":null
{"e_id":"12102","e_password":kkkk,"e_secretQuestion":"abc"}
{"e_id":"12103","e_password":kkkk,"e_secretQuestion":"abc"}
我的jquery:
e.preventDefault();
$.post("/general/helper.php?page=login",$(this).serialize(),function(data,status){
if (data != null){
var tblheader = "<table><tr>";
var tblbody= "";
$.each(data, function(i, field){
tblbody = tblbody + "<td style='border:1px solid gray'>" + field + "</td>";
});
tblbody = tblheader + tblbody + "</tr></table>";
$("#hasil").html(tblbody);
}
},"json");
问题:
如果只返回1行,它的唯一格式就是表格,但如果表格没有格式化更多行... 请帮忙,如何简单地将其格式化为表格?此刻,请不要建议我使用插件将json数组格式化为表格。
感谢
答案 0 :(得分:1)
如果您的data
是已解析的JSON数组,那么当您的代码看起来应该是这样的时候:
e.preventDefault();
$.post("/general/helper.php?page=login",$(this).serialize(),function(data,status){
if (data != null){
var tblbody = "<table>";
$.each(data, function(i, row){
tblbody += '<tr>';
$.each(row, function(i, field) {
tblbody += "<td style='border:1px solid gray'>" + field + "</td>";
});
tblbody += '</tr>';
});
tblbody += "</table>";
$("#hasil").html(tblbody);
}
},"json");