1

通过参考这个链接 ,我尝试实现这个强大的工具并面临一些问题。

每次单击时Ctrl S,它都会弹出一个窗口提示,询问我是否要保存我的 testing.html。

我想忽略 Windows 提示。

我想要的很简单:

  1. 当人们点击保存按钮/使用ctrl s键盘快捷键

  2. 脚本需要做Create()检查

  3. 如果为 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>

代码在这里

4

2 回答 2

3

为避免显示浏览器另存为对话框,您必须阻止默认事件操作,例如纯 jQuery:

$(window).keypress(function(event) {
  if ((event.which == 115 && event.ctrlKey)){
      alert("Ctrl+S pressed");
      event.preventDefault();
  }
});
于 2009-07-13T05:04:04.867 回答
2

您的代码片段没问题,但为了防止默认浏览器操作(如显示保存对话框),您必须捕获KeyPress事件(而不是 KeyDown),当然返回 false

jQuery(document).bind('keypress', 'Ctrl+S',function (evt){ 
 //do job..
 return false
});
于 2009-09-29T08:52:44.477 回答