jQuery嵌套$ .getJSON不工作

问题描述:

我想从数据库中检索一些数据使用jQuery的$.getJSON方法显示在表中,但对于每个用户谁是我得到的信息,我必须得到许多相关的系统相关的用户,这需要在第一个嵌套的第二个$.getJSON调用,我试图变成一个功能,似乎工作,但我不确定如何将系统数据附加到td一旦它通过所有系统循环,这里是代码...

的jQuery:

function systems(id){ 
//here I try to get the bunch of suppliers and loop through them so that all of them display in the one <td> 
    $.getJSON("get_systems.php", {uid:id}, function(data){ 

     $.each(data, function(i, json){ 
      return json.supplier_id; 
     }); 

    }); 
}; 

//on submit get the province and city values, send it to the search_results.php file to get the users, then call the systems() function to display each users systems 
$('#submit').click(function(){ 
    var province_val = $('[data-custom="province"]').val(); 
    var city_val = $('[data-custom="city"]').val(); 

    $.getJSON('search_results.php', { province: province_val, city: city_val }, function(data){ 

     var check = $(data).size(); 

     if(check == 0){ 
      alert("No entries were found"); 
     }else{ 
      $('#table').html(''); 
      $.each(data, function(i, json){ 
       $('#table').append('<tr><td>'+json.company_name+'</td><td>'+json.contact_number+'</td><td>'+json.email_address+'</td><td>'+systems(json.id)+'</td></tr>')      
      }); 
     } 
    }); 
}); 

我认为的代码是非常强的自我explana tory,如果还有其他你想知道的东西,请给我留言。

Thanx提前!

您应该使用id属性,将举行从第二呼叫的结果td元素,并针对它们与..

所以你的第一个填充应

$.each(data, function(i, json){ 
       $('#table').append('<tr><td>'+json.company_name+'</td><td>'+json.contact_number+'</td><td>'+json.email_address+'</td><td id="system_'+i+'"></td></tr>'); 
       systems('system_'+i, json.id);     
      }); 

和你系统功能

function systems(dom_id, id){ 
//here I try to get the bunch of suppliers and loop through them so that all of them display in the one <td> 
    $.getJSON("get_systems.php", {uid:id}, function(data){ 

     $.each(data, function(i, json){ 
      $('#'+dom_id).append(json.supplier_id); 
     }); 

    }); 
}; 
+0

这工作感谢名单,我不得不改变'$(dom_id).append(json.supplier_id);''到$( '#' + dom_id).append(json.supplier _id);',因为它没有指定属性,除了你的方法运行良好外,thanx! – Odyss3us 2010-10-19 13:37:53

+0

@用户,对不起,这是我的错误。编辑未来观点的答案 – 2010-10-19 14:00:18