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

时间:2011-11-29 06:00:06

标签: jquery-ui jquery-ui-dialog

docs我没有看到这样的信息。

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

1)推Esc;

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

但如果点击外面如何关闭对话框?

谢谢!

5 个答案:

答案 0 :(得分:7)

我在ryanjeffords.com找到了解决方案:

<script type="text/javascript">

    $(document).ready(function() {

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

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

答案 1 :(得分:7)

以下是非模态对话框的其他2个解决方案:

如果对话是非模态方法1: 方法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');
                        }
                       }
                      );

非模态对话框方法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;
          }


  });

答案 2 :(得分:5)

如果对话框是模态的,那么在创建对话框选项时,在open函数中粘贴这3行代码:

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

答案 3 :(得分:1)

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

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

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

劳伦

答案 4 :(得分:0)

这是我的解决方案。

我有,例如<​​/ p>

<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');
        }
    }        
});
相关问题