How to completely remove a dialog on close

前端 未结 7 2110
深忆病人
深忆病人 2020-12-02 05:56

When an ajax operation fails, I create a new div with the errors and then show it as a dialog. When the dialog is closed I would like to completely destroy and remove the di

相关标签:
7条回答
  • 2020-12-02 06:23

    You can do use

    $(dialogElement).empty();    
    $(dialogElement).remove();
    
    0 讨论(0)
  • 2020-12-02 06:27

    This is worked for me

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

    Cheers!

    PS: I had a somewhat similar problem and the above approach solved it.

    0 讨论(0)
  • 2020-12-02 06:35

    Why do you want to remove it?

    If it is to prevent multiple instances being created, then just use the following approach...

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

    And when the error occurs, you would do...

    $('#myDialog').html("Ooops.");
    $('#myDialog').dialog('open');
    
    0 讨论(0)
  • 2020-12-02 06:42

    An ugly solution that works like a charm for me:

    $("#mydialog").dialog(
        open: function(){
            $('div.ui-widget-overlay').hide();
            $("div.ui-dialog").not(':first').remove();
    }
    });
    
    0 讨论(0)
  • 2020-12-02 06:42

    I use this function in all my js projects

    You call it: hideAndResetModals("#IdModalDialog")

    You define if:

    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');  
        });
    }
    
    0 讨论(0)
  • 2020-12-02 06:43
    $(this).dialog('destroy').remove()
    

    This will destroy the dialog and then remove the div that was "hosting" the dialog completely from the DOM

    0 讨论(0)
提交回复
热议问题