1

我使用以下代码在按下CTRL+时打开了 JQuery UI 对话框。Q

$(window).keydown(function (e){
     if (e.ctrlKey && e.keyCode == 81){
         $('<div><center>Download the files now?</center></div>').dialog({
            title: '<b>Download</b>', 
            modal: true,
            autoOpen: true,
            height: 400, 
            width: 800,
            resizable: false,
            buttons: {
                "Yes": function(){  
                    // Code to download the Files
                    $(this).dialog('close');
                },
                "Close": function(){
                    $(this).dialog('close');
                }
            }
        });
        return false;
     }
});

但是我怎样才能在再次按下它们时关闭对话框?我想用CTRL+实现对话框的切换效果Q

4

1 回答 1

1

那样的事情怎么样?

var myDialog = null;

$(window).keydown(function (e) {
    if (e.ctrlKey && e.keyCode == 81) {
        if (myDialog != null) {
            myDialog.dialog('close');
            myDialog = null;
        } else {
            var markup = '<div><center>Download the files now?</center></div>';
            myDialog = $(markup).dialog({
                title: '<b>Download</b>', 
                modal: true,
                autoOpen: true,
                height: 400, 
                width: 800,
                resizable: false,
                buttons: {
                    "Yes": function(){  
                        // Code to download the Files
                        $(this).dialog('close');
                    },
                    "Close": function(){
                        $(this).dialog('close');
                    }
                }
            });
        }
        return false;
    }
});

演示:http: //jsfiddle.net/95w4m/

于 2012-06-22T11:57:17.710 回答