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 に答える