Jquery Dialog - div disappears after initialization

…衆ロ難τιáo~ 提交于 2019-11-29 14:07:26

The reason you're seeing it remove #confirmation is because $("#foo").dialog() will move #foo from wherever it is in the DOM to the bottom of the document inside wrapper elements that create the dialog styling which are initially hidden. It's understood that dialogs are hidden until opened.

Okay. I think I have nailed it with the help from you guys.

Some "self-defined" facts about dialog that I know now (Please correct if I am wrong):

  1. When a <div> is initialized as a dialog, it is removed from its original location and moved to the <body> element in a <div class="ui-dialog">. So it 'naturally' disappears.

  2. To select the dialog, you now need a unique identifier. It can be the id in most cases, or some other attributes on that div which makes it unique on the page.

  3. The dialog markup is created every time the dialog is initialized. So, in case of AJAX calls if an already existing dialog is initiated, you will get the popup more than once (as many times it was reinitialized). To solve this, I deleted the existing dialog markups before reintializing. I had to do this because my AJAX response may have some dialogs that need to be initiated.

    function InitializeConfirmationDialog() {
        $('div.confirmation-dialog').each(function() {
        var id = $(this).attr("id");
        if ($('div.ui-dialog-content[id="' + id + '"]').length > 0) {
            $('div.ui-dialog-content[id="' + id + '"]').parents('div.ui-dialog:first').html("").remove();                
        }
        $(this).dialog({
            bgiframe: true, modal: true, autoOpen: false, closeOnEscape: false,
            draggable: true, position: 'center', resizable: false, width: 400, height: 150
        });
    
    
    });
    

    }

in my case a simple 'return false;' in the click function did the trick:

  $("#buttonIdThatOpensTheDialogWhenClicked")
         .button()
         .click(function () {
                $("#myDialog").dialog("open");
                return false;
         });
  });    

Are you sure that one and only one div has id "confirmation"? Duplicate ids aren't allowed.

sigmapi13

The approved answer worked for me too. I was using:

$('.myLink').click(function(){
    $(this).next().dialog(...)
});

and it wasn't there after clicking the first time.

Now I am using the ID direcly:

$("#myId").dialog(...)

and obviously, no matter where dialog moves it on the page, this will find it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!