JQuery- 热键和 Windows 提示问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1117750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JQuery- hotkeys and windows prompt issue
提问by i need help
By referring to http://jshotkeys.googlepages.com/test-static-01.htmlI try to implement this powerful tool and facing some issue.
通过参考http://jshotkeys.googlepages.com/test-static-01.html我尝试实现这个强大的工具并面临一些问题。
everytime when I click Ctrl S, it will popup a window prompt asking me whether I want to save my testing.html
每次我点击Ctrl S,它会弹出一个窗口提示我是否要保存我的testing.html
I want to ignore windows prompt.
我想忽略 Windows 提示。
What I want is simple: 1. when people click save button/ use shortcut key ctrl s from keyboard
我想要的很简单:1.当人们点击保存按钮/使用键盘快捷键 ctrl s
script need to do a Create() checking
if true then continue to submit form, if false, then stop alert Please enter question, focus back to txtQuestion, and don't do any further action.
脚本需要做一个 Create() 检查
如果为 true 则继续提交表单,如果为 false 则停止 alert 请输入问题,焦点回到 txtQuestion,不要做任何进一步的操作。
Below is the full source code for reference: enter
以下是完整的源代码供参考:输入
<html>
<head>
<style>
* {font-family: Helvetica, Verdana, Arial; font-size:0.95em}
.eventNotifier{width: 100px; float: left; color:navy;
border: dotted 1px navy; padding: 4px; background-color:white;
margin:3px}
.dirty{border: solid 1px #0ca2ff; color:white;
background-color:#0ca2ff}
</style>
<script src="jquery-1.3.2.min.js"></script>
<script src="jquery.hotkeys-0.7.9.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
//weird- I found the alert Ctrl+S to appear twice.. ??? $(window).keypress(function(event) { if ((event.which == 115 && event.ctrlKey)){ alert("Ctrl+S pressed"); event.preventDefault(); } });
//奇怪-我发现警报 Ctrl+S 出现两次.. ??? $(window).keypress(function(event) { if ((event.which == 115 && event.ctrlKey)){ alert("Ctrl+S press"); event.preventDefault(); } });
jQuery(document).bind('keydown', 'Ctrl+s',
function(evt){ Create(); return false; });
//jQuery(document).bind('keydown', 'Ctrl+s',
//function (evt){jQuery('#_Ctrl_s'); return false; });
});
function Create()
{
var f = document.frm
if (f.txtQuestion.value.length == 0)
{
alert('Please enter Question.')
f.txtQuestion.focus()
return false
}
f.submit()
}
</script>
</head>
<body>
<form name="frm" method=post action="" >
<div id="_Ctrl_s" class="eventNotifier">Ctrl+s</div>
<input type=text name="txtQuestion" maxlength="255"
class="field400" value="">
<input type=button value="Save" name="BtnSave" onclick="Create()"
class=text100>
</form>
</body>
</html>
code here
代码在这里
回答by CMS
To avoid showing the browser Save As dialog, you must prevent the default event action, example in plain jQuery:
为避免显示浏览器另存为对话框,您必须阻止默认事件操作,例如纯 jQuery:
$(window).keypress(function(event) {
if ((event.which == 115 && event.ctrlKey)){
alert("Ctrl+S pressed");
event.preventDefault();
}
});
回答by Nedko
Your code snippet is ok but in order to prevent default browser action (as showing Save dialog) you must catch the KeyPressevent (not the KeyDown) and of course return false.
您的代码片段没问题,但为了防止默认浏览器操作(如显示保存对话框),您必须捕获KeyPress事件(不是 KeyDown)并且当然返回 false。
jQuery(document).bind('keypress', 'Ctrl+S',function (evt){
//do job..
return false
});