php - 无法显示来自 MySQL 的 DIV 中的数据

标签 php jquery html mysql ajax

我无法显示来自 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/

相关文章:

php - 如何对多行结果的 json 进行编码?

jquery - 截断时调整文本大小

php - css中的路径以在codeigniter中调用背景图像

php - 比较来自 MySQL 表和日志文件的数据

php - 使用PHP检查数据库是否存在于postgreSQL中

jquery - 平滑 jQuery 幻灯片

jquery - jquery中将多个数组合并为一个数组

javascript - 根据php条件隐藏html中的列表元素

python - django 模板帮助,颠倒

html - 如何设置父级的子级 100% 高度(Foundation 6)?