jQuery嵌套$ .getJSON不起作用 [英] jQuery nested $.getJSON not working

查看:468
本文介绍了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>')                     
            });
        }
    });
});

我认为代码是很容易解释的,如果您还有其他想知道的内容,请给我喊一声.

先谢谢!

解决方案

您应该对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);                
            });

您还有系统功能

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);
        });

    });
};

I am trying to retrieve some data from a database using jQuery's $.getJSON method to display in a table, but for each user who's info I get, I have to get the many related systems related to that user, which requires a second $.getJSON call nested in the first, which I have tried turning into a function, which seems to be working, but I am unsure of how to append the system data to the td once it's looped through all of the systems, here is the code...

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>')                     
            });
        }
    });
});

I think the code is pretty self explanatory, if there is anything else you would like to know please gimme a shout.

Thanx in advance!

解决方案

You should use an id attribute to the td elements that will hold the results from the second calls, and target them with that..

so your first populating should be

$.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);                
            });

and you system 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 <td>
    $.getJSON("get_systems.php", {uid:id}, function(data){

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

    });
};

这篇关于jQuery嵌套$ .getJSON不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆