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

时间:2011-11-23 03:53:07

标签: 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
    });
});

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

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

在&#34; cancel&#34;按钮$(this).dialog("close");工作得很好。

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

4 个答案:

答案 0 :(得分: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();
                        }
                    })
                },

答案 1 :(得分:2)

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

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

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

答案 2 :(得分:0)

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

尝试使用var myDiv = $(div);之类的内容,然后您可以随时随地调用它。尽量减少$(this)的使用,以避免出现这种情况。

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

答案 3 :(得分:0)

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

相关问题