我遇到了同样的问题(对话框只会打开一次,关闭后就不会再次打开),并尝试了上面的解决方案,但没有解决我的问题。我回到文档并意识到我对对话框的工作原理有一个根本的误解。
$('#myDiv').dialog() 命令创建/实例化对话框,但不一定是打开它的正确方法。打开它的正确方法是使用 dialog() 实例化对话框,然后使用 dialog('open') 显示它,并使用 dialog('close') 关闭/隐藏它。这意味着您可能希望将 autoOpen 选项设置为 false。
所以过程是:在文档准备好时实例化对话框,然后监听单击或您想要显示对话框的任何操作。然后它会工作,一次又一次!
<script type="text/javascript">
jQuery(document).ready( function(){
jQuery("#myButton").click( showDialog );
//variable to reference window
$myWindow = jQuery('#myDiv');
//instantiate the dialog
$myWindow.dialog({ height: 350,
width: 400,
modal: true,
position: 'center',
autoOpen:false,
title:'Hello World',
overlay: { opacity: 0.5, background: 'black'}
});
}
);
//function to show dialog
var showDialog = function() {
//if the contents have been hidden with css, you need this
$myWindow.show();
//open the dialog
$myWindow.dialog("open");
}
//function to close dialog, probably called by a button in the dialog
var closeDialog = function() {
$myWindow.dialog("close");
}
</script>
</head>
<body>
<input id="myButton" name="myButton" value="Click Me" type="button" />
<div id="myDiv" style="display:none">
<p>I am a modal dialog</p>
</div>