Javascript 基于php响应ajax请求生成动态html表

Javascript 基于php响应ajax请求生成动态html表,javascript,jquery,html,ajax,Javascript,Jquery,Html,Ajax,我有一个页面,它向一个php发送请求以获取一些数据,并响应php的请求返回如下内容:- [{"postID":"1","0":"1","userID":"3","1":"3","imagePath":"images\/31481440272.jpg","2":"images\/3-1481440272.jpg","postDate":"11 December 2016","3":"11 December 2016","postDescription":"trying","4":":D tryin

我有一个页面,它向一个php发送请求以获取一些数据,并响应php的请求返回如下内容:-

[{"postID":"1","0":"1","userID":"3","1":"3","imagePath":"images\/31481440272.jpg","2":"images\/3-1481440272.jpg","postDate":"11 December 2016","3":"11 December 2016","postDescription":"trying","4":":D trying"}]
我想要的是在一个特定的div中创建一个动态html表

<div id="dataWillBeShownHere"></div>

如果记录数超过1,我该怎么办?我应该为每个循环使用多个记录吗

我真的被这件事弄糊涂了。不知何故,我知道解决方案,但无法实施


我希望你理解我的问题

您可以使用简单的javascript和html实现这一点:

<script type="text/javascript">
var records = [];
// This data could be retrieved from an ajax $.post
var data = {"postID":"1","0":"1","userID":"3","1":"3","imagePath":"images\/31481440272.jpg","2":"images\/3-1481440272.jpg","postDate":"11 December 2016","3":"11 December 2016","postDescription":"trying","4":":D trying"};

// Add our data to the records array
records.push(data);

// Get how many record we have
var record_count = records.length;

// Parse data to html tables
for (var i = records.length - 1; i >= 0; i--) {
    var html = "";

    // make your html additions here
    html += "<div> ID: " + records[i]["postID"] + "</div>";
    html += "<div> Date: " + records[i]["postDate"] + "</div>";
    // html += ;
    // html += ;

    // Now use jquery selector to write to the div
    $('#data-table').html(html);
};
</script>


<div id="data-table"></div>

var记录=[];
//此数据可以从ajax$.post中检索
var data={“posted”:“1”,“0”:“1”,“userID”:“3”,“1”:“3”,“imagePath”:“images\/31481440272.jpg”,“2”:“images\/3-1481440272.jpg”,“postDate”:“2016年12月11日”,“3”:“2016年12月11日”,“postDescription”:“trying”,“4”:“D trying”};
//将数据添加到记录数组
记录。推送(数据);
//我们有多少张唱片
var record_count=records.length;
//将数据解析为html表
对于(var i=records.length-1;i>=0;i--){
var html=“”;
//在这里添加html
html++=“ID:”+记录[i][“postID”]+”;
html++=“日期:”+记录[i][“postDate”]+”;
//html+=;
//html+=;
//现在使用jquery选择器写入div
$('#数据表').html(html);
};

检查我的

如果有超过1个结果,那么我肯定会使用循环。对于JSON对象数组,for-each循环听起来很好。我应该如何使用jquery或javascriptwith在特定的div中为每个循环生成html表?没有使用javascriptwell的foreach循环,这很好,但我认为我得到了简单的解决方案。我完全同意。我认为您可以将它们结合在一起,解决方案将通过ajax调用来完成,ajax调用处理对html的写入,并使用jqueryforeach,我通常使用jqueryforeach。我提供for循环的唯一原因是因为我觉得尝试一下会对OP有利。