jQuery UI dialogs: how to close dialog when click outside?

后端 未结 5 1012
夕颜
夕颜 2020-12-15 13:24

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

相关标签:
5条回答
  • 2020-12-15 13:44

    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>
    
    0 讨论(0)
  • 2020-12-15 13:49

    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');
        });
    }
    
    0 讨论(0)
  • 2020-12-15 14:02

    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');
            }
        }        
    });
    
    0 讨论(0)
  • 2020-12-15 14:07

    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;
              }
    
    
      });
    
    0 讨论(0)
  • 2020-12-15 14:08

    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

    0 讨论(0)
提交回复
热议问题