2

我完全是新手,并学会了将我的旧式编码移至这些。现在我需要帮助。

我的 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 数组格式化为表格。

谢谢

4

1 回答 1

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");  
于 2012-11-11T18:55:46.780 回答