如何调用第二jQuery的#第一次和更新页面成功阿贾克斯实例实例、页面、jQuery、阿贾克斯

2023-09-11 00:43:30 作者:゛繁华落幕乄只剩余音

我有一些jQuery是触发点击链接与类changetag。我使用 $。阿贾克斯()更新通过changetag.php数据库。

I have some jQuery that is triggered on click of a link with the class 'changetag'. I'm using $.ajax() to update the database via changetag.php.

我然后通过切换之间的开/关的类更改链接的视觉外观。在code是如下:

I then change the visual appearance of the link by toggling the class between on/off. The code is as follows:

$(function() {
$(".changetag").click(function(){
    var element = $(this);
    var I = element.attr("id");
    var info = 'switch_tag=' + I;

    $.ajax({
        type: "POST",
        url: "_js/changetag.php",
        data: info,
        success: function(){}
    });

    $("#li_"+I).toggleClass("off on");
    element.toggleClass("off on");

    return false;
});
});

完美。但现在我想在第二个PHP调用,这将拉动数据和更新页面的另一个区域,如果上述成功添加。

Works perfectly. But now I want to add in a second PHP call which will pull data and update another area of the page if the above was successful.

我想要补充的是:

$.ajax({
    url: "_js/loaddata.php",
    success: function(results){
        $('#listresults').empty();
        $('#listresults').append(results);
    }
});

但是,仅仅将它添加到成功:函数(){}似乎并不奏效。为了澄清,这里是完整的code我测试:

But just adding it into success: function(){} doesn't seem to be working. To clarify, here is the complete code I'm testing:

$(function() {
$.ajaxSetup ({cache: false});
$(".changetag").click(function(){
    var element = $(this);
    var I = element.attr("id");
    var info = 'switch_tag=' + I;

    $.ajax({
        type: "POST",
        url: "_js/changetag.php",
        data: info,
        success: function(){
            $.ajax({
                url: "_js/loaddata.php",
                success: function(results){
                    $('#listresults').empty();
                    $('#listresults').append(results);
                }
            });
        }
    });

    $("#li_"+I).toggleClass("off on");
    element.toggleClass("off on");

    return false;
});
});

在PHP脚本都称为成功和切换类作品,但拉了数据不写入#listresults出于某种原因。

The PHP scripts are both called successfully and the toggle class works, but the data pulled is not written to #listresults for some reason.

推荐答案

Ajax调用(默认情况下)的同步。这意味着,这个code:

Ajax calls are (by default) asynchronous. That means that this code:

$("#li_"+I).toggleClass("off on");
element.toggleClass("off on");

return false;

可能之前Ajax调用preceding它完成执行。这是程序员谁是新的Ajax和异步code执行一个共同的问题。

could be executed before the ajax call preceding it is finished. This is a common problem for programmers who are new to ajax and asynchronous code execution. Anything you want to be executed after the ajax call is done must be put into a callback, such as your success handler:

$.ajax({
    type: "POST",
    url: "_js/changetag.php",
    data: info,
    success: function(){
        $("#li_"+I).toggleClass("off on");
        element.toggleClass("off on");
    }
});

同样,你可以把第二个Ajax调用在那里还有:

Likewise, you could put the second ajax call in there as well:

$.ajax({
    type: "POST",
    url: "_js/changetag.php",
    data: info,
    success: function(){
        $("#li_"+I).toggleClass("off on");
        element.toggleClass("off on");

        $.ajax({
            url: "_js/loaddeals_v2.php",
            success: function(results){
                $('#listresults').empty();
                $('#listresults').append(results);
            }
        });
    }
});

使用jQuery 1.5的延迟的对象,可以让这个滑头。

With jQuery 1.5's Deferred Object, you can make this slicker.

function firstAjax() {
    return $.ajax({
        type: "POST",
        url: "_js/changetag.php",
        data: info,
        success: function(){
            $("#li_"+I).toggleClass("off on");
            element.toggleClass("off on");
        }
    });
}

// you can simplify this second call and just use $.get()
function secondAjax() {
    return $.get("_js/loaddata.php", function(results){
        $('#listresults').html(results);
    });
}

// do the actual ajax calls
firstAjax().success(secondAjax);

这是很好的,因为它可以让您取消巢回调 - 你可以写$ C异步执行$ C,但这样写同步执行的code

This is nice because it lets you un-nest callbacks - you can write code that executes asynchronously, but is written like synchronously-executed code.