<script type="text/javascript"> var bSave = false; var bClose = true; $(function () { $("#btnOpen").click(function () { bSave = false; //模拟标记数据是否保存,如果没保存,询问是否退出 bClose = false; $("#myDialog").dialog({ title: "测试对话框", width: 600, height: 300, modal: true, onBeforeClose: function () { if (!bSave && !bClose) { $.messager.confirm("提醒", "信息尚未保存,是否退出?", function (r) { if (r) { bClose = true;//标记可以退出 $("#myDialog").dialog("close"); } }); } return bClose; //通过全局变量来控制是否关闭窗口 }, buttons: [{ text: "确定", iconCls: 'icon-ok', handler: function () { bSave = true; bClose = true; $("#myDialog").dialog("close"); } }, { text: "取消", iconCls: "icon-tools", handler: function () { $("#myDialog").dialog("close"); } }] }); }); }); </script>
转载于:https://www.cnblogs.com/lhlong/p/6874210.html