JQuery-热键和Windows提示问题
请参考此链接 我尝试实现这个强大的工具并面临一些问题。
每次我点击Ctrl S
时,都会弹出一个窗口提示询问我是否要保存我的testing.html。
我想忽略 Windows 提示。
我想要的很简单:
当人们点击保存按钮/使用键盘上的快捷键
ctrl s
脚本需要进行
Create()
检查如果为true则继续提交表单,如果为false,则停止警报
请输入问题
,焦点回到txtQuestion
,并且不执行任何进一步操作。
下面是完整的源代码供参考: 在此处输入
<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();
}
});
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>
代码
By referring to this link
I try to implement this powerful tool and facing some issue.
Every time when I click Ctrl S
, it will popup a window prompt asking me whether I want to save my testing.html.
I want to ignore windows prompt.
What I want is simple:
when people click save button/ use shortcut key
ctrl s
from keyboardscript need to do a
Create()
checkingif true then continue to submit form, if false, then stop alert
Please enter question
, focus back totxtQuestion
, and don't do any further action.
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();
}
});
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了避免显示浏览器“另存为”对话框,您必须阻止默认事件操作,例如纯 jQuery 中的示例:
To avoid showing the browser Save As dialog, you must prevent the default event action, example in plain jQuery:
您的代码片段没问题,但为了防止默认浏览器操作(如显示“保存”对话框),您必须捕获 KeyPress 事件(而不是 KeyDown),当然返回 false。
Your code snippet is ok but in order to prevent default browser action (as showing Save dialog) you must catch the KeyPress event (not the KeyDown) and of course return false.