如何用组合键实现切换效果< CTRL + Q>对于Jquery UI对话框?

时间:2012-06-22 11:50:33

标签: jquery user-interface dialog keycode

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

$(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 实现对话框的切换效果。

1 个答案:

答案 0 :(得分: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;
    }
});

DEMO: http://jsfiddle.net/95w4m/

相关问题