2013-11-02 90 views
0

我有两个单独的AJAX调用。一个从txt文件中获取项目列表并从中创建HTML表格并与数据库进行对话以查找每件物品的费用,然后将其列入每个项目的相应表格单元格中(我知道这可能是听起来像一种奇怪的方法,但在我们的案例中这是一个很好的选择......)。修改页面加载后创建的dom元素的html

问题是自从创建表(或者确切地说,创建表的行)后,价格没有写入表中。我不知道如何解决这个问题。

$(document).ready(function() { 
    makeItemTable(); 
    listPrices(); 
    ... 
}); 

function makeItemTable() { 
    $.ajax({ 
     url: 'products.php', 
     type: 'GET' 
     }) 
    .done(function(response) { 
     $('.price-table > tbody').html(response); 
     }) 
} 

function listPrices() { 
    .ajax({ 
     url: 'prices.php', 
     type: 'GET' 
}) 
    .done(function(response) { 
      priceData = $.parseJSON(response); 
      $('.price-table tr').each(function() { 
      var item = $(this).find('td:nth-child(1)').text(); 
      if (priceData[item]) { 
       var price = priceData[item]; 
       $(this).find('td:nth-child(2)').text(price); 
     }  
     }) 
    } 
+2

我会结合您的products.php和prices.php所以你只能做一个请求,而不是两个。但是,如果这是不可能的,请将'listPrices()'移动到'makeItemTable()'ajax调用的成功处理程序中。如果你知道需要多少列,只需创建空列,然后用'listPrices()'填充它们。 Jasen,好点, – Jasen

+0

。原来这个问题实际上是在PHP中。一旦我从txt文件中修剪每个项目的尾部空白,它就可以正常工作。 –

回答

0

您可以尝试

  1. 使用setTimeout的检查要求 'products.php' 执行回调这么做(在回调请求 'prices.php')

  2. 另一种方式

    var jqxhr1 = $.ajax("products.php"); 
    var jqxhr2 = $.ajax("prices.php"); 
    $.when(jqxhr1, jqxhr2).done(function(jqxhr1, jqxhr2) { 
    // Handle both XHR objects 
        alert("all complete");  
    }); 
    
  3. 将回调请求内的函数listPrices()调用到'produc ts.php”

希望能帮到

相关问题