jquery ui Dialog: cannot call methods on dialog prior to initialization

后端 未结 11 1809
既然无缘
既然无缘 2020-11-27 02:52

I have an app on jquery 1.5 with dialogs worked fine. While I have a lot of .live handlers, I changed this to .on. For that, I have to update jquery (now 1.8.3 an jquerui 1.

相关标签:
11条回答
  • 2020-11-27 03:39

    I got this error when I only updated the jquery library without updating the jqueryui library in parallel. I was using jquery 1.8.3 with jqueryui 1.9.0. However, when I updated jquery 1.8.3 to 1.9.1 I got the above error. When I commented out the offending .close method lines, it then threw an error about not finding .browser in the jquery library which was deprecated in jquery 1.8.3 and removed from jquery 1.9.1. So bascially, the jquery 1.9.1 library was not compatible with the jquery ui 1.9.0 library despite the jquery ui download page saying it works with jquery 1.6+. Essentially, there are unreported bugs when trying to use differing versions of the two. If you use the jquery version that comes bundled with the jqueryui download, I'm sure you'll be fine, but it's when you start using different versions that you off the beaten path and get errors like this. So, in summary, this error is from mis-matched versions (in my case anyway).

    0 讨论(0)
  • 2020-11-27 03:44

    If you cannot upgrade jQuery and you are getting:

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

    You can work around it like so:

    $(selector).closest('.ui-dialog-content').dialog('close');
    

    Or if you control the view and know no other dialogs should be in use at all on the entire page, you could do:

    $('.ui-dialog-content').dialog('close');
    

    I would only recommend doing this if using closest causes a performance issue. There are likely other ways to work around it without doing a global close on all dialogs.

    0 讨论(0)
  • 2020-11-27 03:47

    Try this instead

    $(document).ready(function() {
      $("#divDialog").dialog(opt).dialog("open");
    });
    

    You can also do:

    var theDialog = $("#divDialog").dialog(opt);
    theDialog.dialog("open");
    

    That's because the dialog is not stored in $('#divDialog'), but on a new div that is created on the fly and returned by the .dialog(opt) function.

    0 讨论(0)
  • 2020-11-27 03:47

    The new jQuery UI version will not allow you to call UI methods on dialog which is not initialized. As a workaround, you can use the below check to see if the dialog is alive.

    if (modalDialogObj.hasClass('ui-dialog-content')) {
        // call UI methods like modalDialogObj.dialog('isOpen')
    } else {
        // it is not initialized yet
    }
    
    0 讨论(0)
  • 2020-11-27 03:49

    If you want to open the Dialog immediately when the Dialog is initialized or the page is ready, you can also set the parameter autoOpen to true in the options object of dialog:

    var opt = {
            autoOpen: true,
            modal: true,
            width: 550,
            height:650,
            title: 'Details'
    };
    

    Thus, you do not have to call the `$("#divDialog").dialog("open");

    When dialog object is initialized, the dialog is automatically opened.

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