jQuery嵌套的$.getJSON不工作
我试图使用jQuery的jQuery嵌套的$.getJSON不工作,jquery,ajax,json,Jquery,Ajax,Json,我试图使用jQuery的$.getJSON方法从数据库中检索一些数据以显示在表中,但是对于我获取的每个用户信息,我必须获取与该用户相关的许多系统,这需要第二个$.getJSON调用嵌套在第一个调用中,我已尝试将其转换为函数,这似乎是工作,但我不确定如何附加系统数据到td一旦它通过所有的系统循环,这里是代码 jQuery: function systems(id){ //here I try to get the bunch of suppliers and loop through them s
$.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>')
});
}
});
});
功能系统(id){
//在这里,我试图得到一堆供应商,并通过他们循环,使他们所有的显示在一个
$.getJSON(“get_systems.php”,{uid:id},函数(数据){
$.each(数据、函数(i、json){
返回json.supplier\u id;
});
});
};
//在提交获取省和市值时,将其发送到search_results.php文件以获取用户,然后调用systems()函数以显示每个用户的系统
$(“#提交”)。单击(函数(){
var province_val=$('[data custom=“province”]')。val();
var city_val=$('[data custom=“city”]')。val();
$.getJSON('search_results.php',{province:province_val,city:city_val},函数(数据){
变量检查=$(数据).size();
如果(检查==0){
警报(“未找到任何条目”);
}否则{
$('#table').html('');
$.each(数据、函数(i、json){
$('#表')。追加(''+json.company_name++''+json.contact_number++''+json.email_address++''+systems(json.id)+'')
});
}
});
});
我认为代码是非常自解释的,如果还有什么你想知道的,请告诉我
先走一步 您应该对保存第二次调用结果的
td
元素使用id
属性,并以此作为目标
因此,您的第一次填充应该是
$.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);
});
$。每个(数据、函数(i、json){
$(“#表”).append(“”+json.company_name+“”+json.contact_number+“”+json.email_address+“”);
系统('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);
});
});
};
功能系统(dom_id,id){
//在这里,我试图得到一堆供应商,并通过他们循环,使他们所有的显示在一个
$.getJSON(“get_systems.php”,{uid:id},函数(数据){
$.each(数据、函数(i、json){
$('#'+dom_id).append(json.supplier_id);
});
});
};
这对thanx有效,我只需更改$(dom\u id).append(json.supplier\u id)
到$('#'+dom_id).append(json.supplier_id)代码>,因为它没有指定属性,但除此之外,您的方法工作得很好,thanx@用户,很抱歉。。这是我的一个错误。为将来的视图编辑了答案。。