当我在实际对话框外单击时,如何关闭jQuery对话框?

时间:2014-01-23 09:40:53

标签: javascript jquery html asp.net webforms

我从按钮单击启动一个jQuery模式窗口,并希望在我单击对话框外的某个位置时关闭它。有谁知道如何实现这一目标?

这是对话框的创建方式:

var $dial2 = ""


    function openmenu(width, height, menuID){ if ($dial2 == "") {
        $dial2 = $('<div></div>')
                       .html('<iframe id="dial2" style="border: 0px; " src=/CustomControls/PageSubMenu.aspx?&menuID="' + menuID + '" width="100%" height="100%"></iframe>')
                       .dialog({
                           autoOpen: false,
                           modal: false,
                           height: height,
                           width: width,
                           title: 'Menu',
                           draggable: false,
                           resizable: false,
                           position: {
                               my: 'top',
                               at: 'left-207',
                               of: $('#btnMenu')
                           },
                           dialogClass: "testdia"
                       });
        $dial2.dialog('open');
    }
}

只是为了让你知道我已经尝试了在stackoverflow上的帖子中显示的选项(2 x非模态),但它们不起作用,这就是我发布这个的原因。我想这与iframe等有关,所以这是一个特例。

2 个答案:

答案 0 :(得分:0)

您可以将clickoutside关闭事件绑定到对话框对象

编辑:这不起作用,因为您有位置值of:$('#btnMenu'),但没有这样的元素。我添加了一个元素,您可以在这里查看工作小提琴:http://jsfiddle.net/53v5E/

var $dial2 = ""
var closed = 0;

function openmenu(width, height, menuID) {
    if ($dial2 == "") {
        $dial2 = $('<div></div>')
            .html('<iframe id="dial2" style="border: 0px; " src="http://jsfiddle.net/echo/jsonp/" width="100%" height="100%"></iframe>')
            .dialog({
            autoOpen: false,
            modal: false,
            height: height,
            width: width,
            title: 'Menu',
            draggable: false,
            position: {
                my: 'top',
                at: 'left-207',
                of: $('#btnMenu')
            },
            resizable: false,
            open: function (e, ui) {
                closed = 0;
                $(document).bind('click', closeDialog);
            }
        });
        $dial2.dialog('open');
    }
}

function closeDialog() {
    if (closed == 0) {
        $dial2.dialog('close');
        closed = 1;
    }
}

openmenu("500", "500", "5")

答案 1 :(得分:0)

您可以使用文档的单击事件来关闭对话框。 在这里,我绑定了一个代码,以便更好地理解。

var $dial2 = ""

function openmenu(width, height, menuID){ if ($dial2 == "") {
   $dial2 = $('<div></div>')
             .html('<iframe id="dial2" style="border: 0px; " src=/CustomControls/PageSubMenu.aspx?&menuID="' + menuID + '" width="100%" height="100%"></iframe>')
             .dialog({
                autoOpen: false,
                modal: false,
                height: height,
                width: width,
                title: 'Menu',
                draggable: false,
                resizable: false,
                position: {
                   my: 'top',
                   at: 'left-207',
                   of: $('#btnMenu')
                },
                dialogClass: "testdia"
             });
             $dial2.bind('click',function(e){
                e.stopPropagation();
             });
             $dial2.dialog('open');

             $(document).bind('click',function(){
                $dial2.dialog('close');
             });
          }
      }