根据php对ajax请求的响应制作动态html表
16lz
2021-01-22
I have page which send request to a php for getting some data and in response to that request of php returns me something like this:-
我有页面发送请求到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"}]
What I want is to make a dynamic html table inside a specific div
我想要的是在特定div中创建一个动态html表
<div id="dataWillBeShownHere"></div>
What will I do if records will be more than 1.Should I use for each loop for more than one record.
如果记录超过1,我该怎么办。我应该为每个循环使用多个记录。
I am really confused in this thing. Somehow I know the solution but cannot implement.
我真的很困惑这件事。不知怎的,我知道解决方案,但无法实现。
I hope you have understand my question.
我希望你能理解我的问题。
1 个解决方案
#1
1
You can do this with simple javascript and html:
你可以用简单的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>
Check my JSFiddle
检查我的JSFiddle
更多相关文章
- 等待执行所有ajax回调的最佳解决方案
- 【问题解决方案】ImportError: No module named 'pygal'
- Python 黏包及黏包解决方案
- 无法安装ndg-httpsclient或者我的解决方案错误
- Linux无法连接网络解决方案
- Media-S 简介(一个开源的DRM解决方案)
- AppScan安全问题解决方案
- PLSQL乱码解决方案
- 数据库不支持中文解决方案(mysql)