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 $.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(' '+json.company_name+' '+json.contact_number+' '+json.email_address+' '+systems(json.id)+' ') }); } }); });
我认为代码是非常自我解释的,如果还有其他任何你想知道的话请给我一个大喊大叫。
Thanx提前!
你应该使用id
属性来保存第二次调用结果的td
元素,然后用它来定位它们。
所以你的第一次填充应该是
$.each(data, function(i, json){ $('#table').append(''+json.company_name+' '+json.contact_number+' '+json.email_address+' '); systems('system_'+i, json.id); });
和你的系统function
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 $.getJSON("get_systems.php", {uid:id}, function(data){ $.each(data, function(i, json){ $('#'+dom_id).append(json.supplier_id); }); }); };