admin管理员组文章数量:1417675
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
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
Share Improve this question edited Aug 28, 2020 at 7:37 Alyona Yavorska 5792 gold badges14 silver badges20 bronze badges asked Jul 13, 2009 at 4:51 i need helpi need help 2,38610 gold badges55 silver badges73 bronze badges2 Answers
Reset to default 3To avoid showing the browser Save As dialog, you must prevent the default event action, example in plain jQuery:
$(window).keypress(function(event) {
if ((event.which == 115 && event.ctrlKey)){
alert("Ctrl+S pressed");
event.preventDefault();
}
});
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.
jQuery(document).bind('keypress', 'Ctrl+S',function (evt){
//do job..
return false
});
本文标签: javascriptJQuery hotkeys and windows prompt issueStack Overflow
版权声明:本文标题:javascript - JQuery- hotkeys and windows prompt issue - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745277093a2651249.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论