我有一个庞大的Javascript文件,可以在应用程序的各种屏幕上捕获各种键盘。我总是使用return false();
来防止默认的keydown操作发生,这很有效。
但是,在这个特定的keydown上,confirm()
之前有一个return false;
框。如果我点击C
,则会出现confirm()
对话框。如果我将其移开,在其下方,c
已经写入文本框。
如何成功抑制keydown?这只需要在Chrome中使用。
document.onkeydown = keyDown;
function keyDown()
{
switch(window.event.keyCode)
{
case(67):
if(confirm("WARNING: You have unsaved information - in order to invoice you must first save!\n\nOK/Enter: Save and proceed to invoice\nCancel/Esc: Do NOT save and don't invoice"))
alert('Do something...');
else
return false;
window.location = "/invoice-12345";
return false;
break;
}
}
答案 0 :(得分:1)
您可以使用setTimeout延迟确认。 jsFiddle
document.onkeydown = keyDown;
function keyDown()
{
switch(window.event.keyCode)
{
case(67):
setTimeout(function(){
if(confirm("WARNING: You have unsaved information - in order to invoice you must first save!\n\nOK/Enter: Save and proceed to invoice\nCancel/Esc: Do NOT save and don't invoice"))
alert('Do something...');
else
return false;
window.location = "/invoice-12345";
},0);
return false;
break;
}
}