javascript - JQuery-热键和windows提示问题

标签 javascript jquery windows hotkeys prompt

引用这个link 我尝试实现这个强大的工具并遇到一些问题。

每次当我点击 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>

代码在这里

最佳答案

要避免显示浏览器另存为对话框,您必须阻止默认事件操作,在纯 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/

相关文章:

javascript - 如何让 QUnit 等待一定的时间间隔?

Javascript str.replace 变量

jquery - 通过 document.write 调用 Google Charts : A Parser-blocking, 跨站点

C - 构建/编译/部署成功但在其他机器上失败

windows - 失去本地主机网络连接的机会?

javascript - 如何使用 lodash 在数组中插入特定索引?

javascript - 站点加载后向 Piwik 添加自定义 session 参数

javascript - 如何在带有 iframe 的页面中仅附加到主体标签?

javascript - 如何在office 365 .js 文件中调用服务器端方法

c - 为什么 Mingw32 提供的 WinBase.h 不包括 Windows 7 的 `SetProcessDEPPolicy'?