javascript 无法在ajax成功内关闭对话框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8237088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 02:45:16  来源:igfitidea点击:

can't close dialog box within ajax success

javascriptjquery

提问by themerlinproject

I'm calling a dialog box on the fly (on click), not setting it up as a var first. Like so:

我正在即时调用对话框(单击时),而不是先将其设置为 var。像这样:

$(".deleteSaved").click(function() {
        save_id = $(this).attr('id');


    div="<div>Are you sure you want to delete this?</div>";
    $(div).dialog({ 
        buttons: { 
            "Delete": function() { 
                $.ajax ({
                    url:"util.php",
                    data:"q=0&f=delete&save_id="+save_id,
                    success: function(result){
                        $(this).dialog("close"); //this line is not working
                        $("#toprow"+save_id).fadeOut();
                        $("#botrow"+save_id).fadeOut();
                    }
                })
            },
            "Cancel": function() { 

                $(this).dialog("close");
            } 
        },
        modal: true,
        title: 'Delete Saved Signal',
        resizable: false
    });
});

But when I call $(this).dialog("close");within the ajax success function I get the following error:

但是当我$(this).dialog("close");在 ajax 成功函数中调用时,我收到以下错误:

Uncaught cannot call methods on dialog prior to initialization; attempted to call method 'close'

Within the "cancel" button $(this).dialog("close");works just fine.

在“ cancel”按钮内$(this).dialog("close");工作得很好。

How can I get the close function to work within the ajax success call?

如何让 close 函数在 ajax 成功调用中工作?

回答by robert

Inside success function, 'this' is not pointing to dialog object. You may have to store the dialog object in another variable, like shown below

在成功函数中,'this' 不指向对话框对象。您可能必须将对话框对象存储在另一个变量中,如下所示

"Delete": function() { 
                 var that = this;
                    $.ajax ({
                        url:"util.php",
                        data:"q=0&f=delete&save_id="+save_id,
                        success: function(result){
                            $(that).dialog("close"); //this line will work
                            $("#toprow"+save_id).fadeOut();
                            $("#botrow"+save_id).fadeOut();
                        }
                    })
                },

回答by Jayantha Lal Sirisena

You can't refer it using $(this) because you are in another function , You can do like this,

你不能使用 $(this) 引用它,因为你在另一个函数中,你可以这样做,

div="<div id='yourDialog'>Are you sure you want to delete this?</div>";
//...........    

     $("#yourDialog").dialog("close");

回答by Mario Cesar

The Scope on the "success" function isn't the same as the scope for the "Delete" or "Cancel" functions...

“成功”功能的范围与“删除”或“取消”功能的范围不同......

Try using something like var myDiv = $(div);and then you can call it wherever you want. Minimize the use of the $(this)to avoid this sort of situations.

尝试使用类似的东西var myDiv = $(div);,然后你可以在任何你想要的地方调用它。尽量减少使用$(this)以避免这种情况。

Also, just a quick tip, instead of $(this).attr('id')use this.id;)

另外,只是一个快速提示,而不是$(this).attr('id')使用this.id;)

回答by Jeff Lauder

Your $(this) has a different meaning within the success function. Try assigning it to a variable and using that within your ajax success function

您的 $(this) 在成功函数中具有不同的含义。尝试将它分配给一个变量并在您的 ajax 成功函数中使用它