Simple jQueryUI Dialog causes error with 1.9

蹲街弑〆低调 提交于 2019-12-12 03:36:15

问题


I've previously used the following script and it worked perfectly. I am now getting the following error:

Error: cannot call methods on dialog prior to initialization; attempted to call method 'close'.
jquery-latest.js (line 507)

If you upgrade to jQueryUI 1.10.1, it no longer causes an error. Normally, I would just let it go, upgrade, and not worry about it. Problem with this one is that it worked in the past so why not now is driving me crazy? I must be doing some silly thing different than previously, but I cannot see it.

<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>jQuery UI Dialog</title>
        <script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script> 
        <link type="text/css" href="http://code.jquery.com/ui/1.9.0/themes/base/jquery-ui.css" rel="stylesheet" />
        <script src="http://code.jquery.com/ui/1.9.0/jquery-ui.js" type="text/javascript"></script> 
        <script>
            $(function() {
                $("#open").click(function(){$("#dialog").dialog('open');return false;});
                $( "#dialog" ).dialog({
                    modal: true,
                    autoOpen    : false,
                    buttons: {
                        Ok: function() {
                            $( this ).dialog( "close" );
                        }
                    }
                });
            });
        </script>
    </head>
    <body>
        <button id="open">Click</button>
        <div id="dialog" title="What ever"></div>
    </body>
</html>

回答1:


I had a similar problem that i resolved by defining my button array outside of the dialog declaration.

var buttonArray = {};
buttonArray["Ok"]=function() { $( this ).dialog( "close" );}

Your options would become:

     modal: true,
     autoOpen: false,
     buttons: buttonArray


来源:https://stackoverflow.com/questions/15188039/simple-jqueryui-dialog-causes-error-with-1-9

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