简体   繁体   English

如何通过单击框所覆盖的区域外单击关闭jQuery UI模式对话框?

[英]How to close a jQuery UI modal dialog by clicking outside the area covered by the box?

I am using jQuery 1.6 and jQuery UI. 我正在使用jQuery 1.6和jQuery UI。 I successfully implemented a modal dialog window which width is almost 50% of my application web page' width. 我成功实现了一个模式对话框窗口,其宽度几乎是我的应用程序网页宽度的50%。 I would like to give to the user another way to close the dialog so that when he\\she clicks outside the area covered on the page by the "modal box", this one will be closed as if the user clicked on the "standard" "x" button on the top-right of that. 我想给用户另一种关闭对话框的方法,这样当他点击“模态框”在页面上覆盖的区域外时,这个将被关闭,好像用户点击了“标准”右上方的“x”按钮。

How can I do that? 我怎样才能做到这一点?

Update 更新

This was the initial answer: 这是最初的答案:

$(".ui-widget-overlay").click (function () {
     $("#your-dialog-id").dialog( "close" );
});

This is the working solution: 这是工作解决方案:

$('.ui-widget-overlay').live('click', function() {
     $('#your-dialog-id').dialog( "close" );
});

How about this way,this way you can close the dialog whatever it open from where and which id. 这种方式怎么样,这样你可以关闭对话框,无论它从哪里打开,哪个id。 It's a global function: 这是一个全球功能:

   $("body").on("click",".ui-widget-overlay",function() {
     $(".ui-dialog-titlebar-close").click();
   });

To clarify, the answer by Victor only works if the dialog is set to autoOpen: true , the default value of the dialog, and you do not open the dialog again with an event. 为了澄清,Victor的答案仅在对话框设置为autoOpen: true ,对话框的默认值, 并且不会再次使用事件打开对话框。 If you open the dialog with an event like click at any point whether autoOpen is set to true or false , then you have to use jQuery.live . 如果您打开一个事件对话框,例如在任何时候click autoOpen是设置为true还是false ,那么您必须使用jQuery.live

Fiddle demonstrating failure of overlay click event with autoOpen: false : http://jsfiddle.net/GKfZM/ 使用autoOpen: false演示覆盖click事件失败的小autoOpen: falsehttp//jsfiddle.net/GKfZM/

Fiddle demonstrating how live works with autoOpen: false and with click event: http://jsfiddle.net/GKfZM/1/ 小提琴演示了如何live的作品具有autoOpen: falseclick事件: http://jsfiddle.net/GKfZM/1/

Summary: Victor's answer only works under certain conditions. 摘要: Victor的回答仅适用于某些条件。

Tutorial link 教程链接

You have two options for the close model dialog box 关闭模型对话框有两个选项

$('#your-dialog-id').modal('toggle');

OR 要么

you can use the click event directly when you click outside box 单击外部框时,可以直接使用单击事件

$("#your-dialog-id .close").click()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM