如何在关闭时完全删除对话框

时间:2010-05-19 10:42:56

标签: jquery-ui jquery-ui-dialog

当ajax操作失败时,我创建一个带有错误的新div,然后将其显示为对话框。当对话框关闭时,我想彻底销毁并再次移除div。我怎样才能做到这一点?我的代码目前看起来像这样:

$('<div>We failed</div>')
    .dialog(
    {
        title: 'Error',
        close: function(event, ui)
        {
            $(this).destroy().remove();
        }
    });

当我运行此对话框时,对话框显示正确,但是当我关闭它时,对话框仍然可以在html中看到(使用FireBug)。我在这里错过了什么?我遗忘了什么?

更新:注意到我的代码在firebug控制台中出现错误。

  

$(this).destroy不是函数

任何人都可以帮助我吗?

更新:如果我只改为$(this).remove(),则会从html中删除该项目。但它是否完全从DOM中删除?或者我是否还需要先调用destroy函数?

7 个答案:

答案 0 :(得分:256)

$(this).dialog('destroy').remove()

这会破坏对话框,然后从DOM中删除完全“托管”对话框的div

答案 1 :(得分:10)

为什么要删除它?

如果是为了防止创建多个实例,那么只需使用以下方法......

$('#myDialog') 
    .dialog( 
    { 
        title: 'Error', 
        close: function(event, ui) 
        { 
            $(this).dialog('close');
        } 
    }); 

当错误发生时,你会......

$('#myDialog').html("Ooops.");
$('#myDialog').dialog('open');

答案 2 :(得分:8)

$(dialogElement).empty();

$(dialogElement).remove();

这可以将其修复为真正的

答案 3 :(得分:3)

这对我有用

$('<div>We failed</div>')
    .dialog(
    {
        title: 'Error',
        close: function(event, ui)
        {
            $(this).dialog("close");
            $(this).remove();
        }
    });

干杯!

PS:我有一个类似的问题,上面的方法解决了它。

答案 4 :(得分:2)

一个丑陋的解决方案,对我来说就像一个魅力:

$("#mydialog").dialog(
    open: function(){
        $('div.ui-widget-overlay').hide();
        $("div.ui-dialog").not(':first').remove();
}
});

答案 5 :(得分:1)

你可以使用

$(dialogElement).empty();    
$(dialogElement).remove();

答案 6 :(得分:0)

我在所有js项目中都使用了此功能

您称它为:     hideAndResetModals(“#IdModalDialog”)

您定义是否:

function hideAndResetModals(modalID)
{
    $(modalID).modal('hide');
    clearValidation(modalID); //You implement it if you need it. If not, you can remote this line
    $(modalID).on('hidden.bs.modal', function () 
    {
        $(modalID).find('form').trigger('reset');  
    });
}
相关问题