我有以下代码,当单击链接时,该代码显示确认div。我第一次运行它,当ajax请求完成时,deleteConfirmation div会滑动。但是,当我第二次尝试(不刷新)时,确认div每次向下滑动时都会向上滑动,即使未触发此点击也是如此。

$("#deleteConfirm").click(function() {

    if(debug)
        console.log("deleting "+$("#deleteConfirmationCampaign").html());

    $("#mappingsRemove").html("Deleting Campaign and Mappings...");

    // remove each mapping underneath the campaign
    deleteCampaign(apiUrl, "mappings", account+"/"+campaignToDelete[1], "confirmDelete");

    $(document).ajaxStop(function() {

        $("#mappingsRemove").html("Finished");

        // slide up the delete confirmation dialog
        $("#deleteConfirmation").slideUp();

    });

});


我的deleteCampaign函数中有几个请求,我使用不正确吗?

我认为发生的事情是第一次触发它为ajaxStop设置全局侦听器。当然,这总是会触发,因此它总是尝试滑动确认div。也许我需要在ajaxStop第一次滑动后停止它的文档监听器?

任何建议将帮助您!

最佳答案

您对发生的事情是正确的。但是我不确定ajaxStop是听的最好的事件。所有请求完成后就会触发,对吧?

我认为您只希望对与deleteCampain函数相关的请求做出反应。

当所有重要请求都返回时,您可以使用延期执行函数。

var ajax1 = $.ajax("/deleteCampain.php"),
    ajax2 = $.ajax("/deleteCampain2.php");

$.when(ajax1, ajax2).then(function(){
    $("#mappingsRemove").html("Finished");
    $("#deleteConfirmation").slideUp();
});


看到:

http://api.jquery.com/category/deferred-object/

http://api.jquery.com/jQuery.when

10-07 17:29