引用这个link 我尝试实现这个强大的工具并遇到一些问题。
每次当我点击 Ctrl S
时,它都会弹出一个窗口提示询问我是否要保存我的 testing.html。
我想忽略 windows 提示。
我想要的很简单:
当人们点击保存按钮/使用键盘上的快捷键
ctrl s
脚本需要做一个
Create()
检查如果为true则继续提交表单,如果为false则停止alert
Please enter question
,焦点回到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>
代码在这里
最佳答案
要避免显示浏览器另存为对话框,您必须阻止默认事件操作,在纯 jQuery 中的示例:
$(window).keypress(function(event) {
if ((event.which == 115 && event.ctrlKey)){
alert("Ctrl+S pressed");
event.preventDefault();
}
});
关于javascript - JQuery-热键和windows提示问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1117750/