我无法显示来自 MySQL 数据库的 DIV 集中的数据。 数据只显示在最后一个 DIV 中,下面是我的代码:
$(document).ready(function (){
var n = 9;
for(var i=0;i<n;i++){
var div = document.createElement('div');
div.className = "d5";
div.id=i+1;
document.getElementById('container').appendChild(div);
$.ajax({
url: 'myapi.php', //the script to call to get data
data: "", //you can insert url argumnets here to pass to api.php for example "id=5&parent=6"
dataType: 'json', //data format
success: function(data) //on recieve of reply
{
var Name = data[2]; //get id
//var vname = data[1]; //get name
$('#'+div.id).html(""+Name);
}
});
}
});
最佳答案
当你发出 ajax 请求时,再添加一个属性,async:false。希望你得到你的结果
$.ajax({
url: 'myapi.php', //the script to call to get data
data: "",
async:false , //you can insert url argumnets here to pass to api.php for example "id=5&parent=6"
dataType: 'json', //data format
success: function(data) //on recieve of reply
{
var Name = data[2]; //get id
//var vname = data[1]; //get name
$('#'+div.id).html(""+Name);
}
});
关于php - 无法显示来自 MySQL 的 DIV 中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28344786/