最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JQuery- hotkeys and windows prompt issue - Stack Overflow

programmeradmin0浏览0评论

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:

  1. when people click save button/ use shortcut key ctrl s from keyboard

  2. script need to do a Create() checking

  3. if true then continue to submit form, if false, then stop alert Please enter question, focus back to txtQuestion, 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:

  1. when people click save button/ use shortcut key ctrl s from keyboard

  2. script need to do a Create() checking

  3. if true then continue to submit form, if false, then stop alert Please enter question, focus back to txtQuestion, 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 badges
Add a ment  | 

2 Answers 2

Reset to default 3

To 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
});
发布评论

评论列表(0)

  1. 暂无评论