jQuery UI 对话框:单击外部时如何关闭对话框?

发布于 2024-12-18 21:08:55 字数 216 浏览 1 评论 0原文

docs 中我没有看到此类信息。

在这种情况下,可以选择关闭对话框:

1) 按 Esc;

2) 单击对话框中的“确定”或“关闭”按钮。

但是如果点击外部如何关闭对话框呢?

谢谢!

In docs I didn't see such information.

There are options to close dialog in such cases:

1) push Esc;

2) click on "OK" or "Close" buttons in the dialog.

But how to close dialog if click outside?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

花桑 2024-12-25 21:08:56

如果对话框是模式对话框,则在创建对话框选项时将这 3 行代码粘贴到 open 函数中:

open: function(event,ui) {
    $('.ui-widget-overlay').bind('click', function(event,ui) {         
        $('#myModal').dialog('close');
    });
}

If dialog is modal, then paste these 3 lines of code in the open function when you create your dialog options:

open: function(event,ui) {
    $('.ui-widget-overlay').bind('click', function(event,ui) {         
        $('#myModal').dialog('close');
    });
}
别再吹冷风 2024-12-25 21:08:56

面对同样的问题,我创建了一个小插件,可以在单击对话框外部时关闭对话框,无论是模式对话框还是非模式对话框。它支持同一页面上的一个或多个对话框。

有关我网站的更多信息: http:// www.coheractio.com/blog/looking-jquery-ui-dialog-widget-when-clicking-outside

该插件也在 github 上:https://github.com/coheractio/jQuery-UI-Dialog-ClickOutside

劳伦特

Facing the same problem, I have created a small plugin that enables to close a dialog when clicking outside of it whether it a modal or non-modal dialog. It supports one or multiple dialogs on the same page.

More information on my website here: http://www.coheractio.com/blog/closing-jquery-ui-dialog-widget-when-clicking-outside

The plugin is also on github: https://github.com/coheractio/jQuery-UI-Dialog-ClickOutside

Laurent

但可醉心 2024-12-25 21:08:56

这是我的解决方案。

例如,我有

<div id="dialog1">Some content in here</div>
<div id="dialog2">Different content in here</div>
<div id="dialog3">And so on...</div>

每个 div 作为一个对话框打开,具体取决于用户交互的内容。因此,为了能够关闭当前活动的,我这样做了。

// This closes the dialog when the user clicks outside of it.
$("body").on('click', '.ui-widget-overlay', function() {
    if( $("div.ui-dialog").is(":visible") )
    {
        var openDialogId = $(".ui-dialog").find(".ui-dialog-content:visible").attr("id");
        if ($("#"+openDialogId).dialog("isOpen"))
        {
            $("#"+openDialogId).dialog('close');
        }
    }        
});

This is my solution.

I have, for example

<div id="dialog1">Some content in here</div>
<div id="dialog2">Different content in here</div>
<div id="dialog3">And so on...</div>

Each div gets opened as a dialog depending on what the user interacts with. So being able to close the currently active one, I do this.

// This closes the dialog when the user clicks outside of it.
$("body").on('click', '.ui-widget-overlay', function() {
    if( $("div.ui-dialog").is(":visible") )
    {
        var openDialogId = $(".ui-dialog").find(".ui-dialog-content:visible").attr("id");
        if ($("#"+openDialogId).dialog("isOpen"))
        {
            $("#"+openDialogId).dialog('close');
        }
    }        
});
煞人兵器 2024-12-25 21:08:55

以下是非模态对话框的另外 2 种解决方案:

如果对话框是非模态的 方法 1:
方法一: http://jsfiddle.net/jasonday/xpkFf/

// Close Pop-in If the user clicks anywhere else on the page
                     jQuery('body')
                      .bind(
                       'click',
                       function(e){
                        if(
                         jQuery('#dialog').dialog('isOpen')
                         && !jQuery(e.target).is('.ui-dialog, a')
                         && !jQuery(e.target).closest('.ui-dialog').length
                        ){
                         jQuery('#dialog').dialog('close');
                        }
                       }
                      );

非模态对话框 方法二:
http://jsfiddle.net/jasonday/eccKr/

  $(function() {
            $( "#dialog" ).dialog({
                autoOpen: false, 
                minHeight: 100,
                width: 342,
                draggable: true,
                resizable: false,
                modal: false,
                closeText: 'Close',
                  open: function() {
                      closedialog = 1;
                      $(document).bind('click', overlayclickclose);
                  },
                  focus: function() {
                      closedialog = 0;
                  },
                  close: function() {
                      $(document).unbind('click');
                  }



        });

         $('#linkID').click(function() {
            $('#dialog').dialog('open');
            closedialog = 0;
        });

         var closedialog;

          function overlayclickclose() {
              if (closedialog) {
                  $('#dialog').dialog('close');
              }

              //set to one because click on dialog box sets to zero
              closedialog = 1;
          }


  });

Here are 2 other solutions for non-modal dialogs:

If dialog is non-modal Method 1:
method 1: http://jsfiddle.net/jasonday/xpkFf/

// Close Pop-in If the user clicks anywhere else on the page
                     jQuery('body')
                      .bind(
                       'click',
                       function(e){
                        if(
                         jQuery('#dialog').dialog('isOpen')
                         && !jQuery(e.target).is('.ui-dialog, a')
                         && !jQuery(e.target).closest('.ui-dialog').length
                        ){
                         jQuery('#dialog').dialog('close');
                        }
                       }
                      );

Non-Modal dialog Method 2:
http://jsfiddle.net/jasonday/eccKr/

  $(function() {
            $( "#dialog" ).dialog({
                autoOpen: false, 
                minHeight: 100,
                width: 342,
                draggable: true,
                resizable: false,
                modal: false,
                closeText: 'Close',
                  open: function() {
                      closedialog = 1;
                      $(document).bind('click', overlayclickclose);
                  },
                  focus: function() {
                      closedialog = 0;
                  },
                  close: function() {
                      $(document).unbind('click');
                  }



        });

         $('#linkID').click(function() {
            $('#dialog').dialog('open');
            closedialog = 0;
        });

         var closedialog;

          function overlayclickclose() {
              if (closedialog) {
                  $('#dialog').dialog('close');
              }

              //set to one because click on dialog box sets to zero
              closedialog = 1;
          }


  });
冷血 2024-12-25 21:08:55

我在 ryanjeffords.com

<script type="text/javascript">

    $(document).ready(function() {

        $("#dialog").dialog();

        $('.ui-widget-overlay').live("click",function(){
            $("#dialog").dialog("close");
        });    
    });   
</script>

I found solution on ryanjeffords.com:

<script type="text/javascript">

    $(document).ready(function() {

        $("#dialog").dialog();

        $('.ui-widget-overlay').live("click",function(){
            $("#dialog").dialog("close");
        });    
    });   
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文