我在使用以下代码按 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 实现对话框的切换效果。
答案 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;
}
});