我该如何检测文件输入对话框是否打开?
我正在尝试将一些文件上传功能集成到一个弹出式(bootstrap样式)模型中,在我正在构建的Web应用程序中。作为模型的行为的一部分,如果按下ESC键,模型将关闭。
这都很好,直到我从打开的模型中打开文件输入对话框,如果我按下ESC键关闭输入对话框,模型也将关闭。
我正在尝试实现的一个超级简化版http://jsfiddle.net/ckevy/1/
我该如何检测文件输入对话框是否打开?
我正在尝试将一些文件上传功能集成到一个弹出式(bootstrap样式)模型中,在我正在构建的Web应用程序中。作为模型的行为的一部分,如果按下ESC键,模型将关闭。
这都很好,直到我从打开的模型中打开文件输入对话框,如果我按下ESC键关闭输入对话框,模型也将关闭。
我正在尝试实现的一个超级简化版http://jsfiddle.net/ckevy/1/
// esc must close popup
$("body").keyup(function(e) {
if (27 == e.keyCode) {
hidepopup();
}
});
// input in popup
var $file = $("input:file");
// keyup will be catched for input, not for body
$file.bind("click", function(e) {
$(this).focus();
});
// keyup catching will be changed back to body after selecting file
$file.bind("change", function(e) {
$(this).blur();
});
// we catched esc keyup, so change esc catching back to the body
$file.keyup(function(e) {
if (27 == e.keyCode) {
$(this).blur();
// i don't know, why it works with return false, because i am not js programmer ), but cancelBubble or e.preventDefault is not working
return false;
}
});
我不相信你实际上能够直接控制对话框本身。在某些浏览器中,比如火狐浏览器,人们已经能够在一定程度上操纵对话框,但这并不适用于所有浏览器和所有浏览器版本。
最简单的方法是在打开文件窗口之前禁用模态对话框上的快捷键。
$('input[type="file"]').on('keydown',function(e){
//Prevents code from firing if file browser is open
if($(this).is(':focus')){
//run code here that should only be applied when the dialog box is closed
}
});