无法在ajax成功中关闭对话框

the*_*ect 4 javascript jquery

我正在快速调用一个对话框(点击),而不是先将其设置为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
    });
});
Run Code Online (Sandbox Code Playgroud)

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

Uncaught cannot call methods on dialog prior to initialization; attempted to call method 'close'
Run Code Online (Sandbox Code Playgroud)

在" cancel"按钮内$(this).dialog("close");工作正常.

如何在ajax成功调用中使用close函数?

rob*_*ert 11

在内部成功函数中,'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();
                        }
                    })
                },
Run Code Online (Sandbox Code Playgroud)