Working with the Enter / Return Key in Chrome

I have a page like AJAX-y. When the user presses "GO", I need to execute a specific javascript function. I also want to simulate a “GO” click when the user presses “Enter” or “Return”. Please note that I do not want to “submit” the page. The reason is because I am doing everything with JavaScript.

To process the "Enter" / "Return" key, click in IE and FireFox, I use the following code:

$(document).ready(function () {  
  $('#root').keypress(function(e) { 
    if (e.keyCode == '13') { 
      goButton(); 
    } 
  });
});

Unfortunately, this code does not work in Chrome. The goButton () function is called. The problem is that the page is being sent. What am I doing wrong?

+5
source share
3 answers

, :

$('#theForm').submit(function(e){
    e.preventDefault();
    goButton();
    return false; // just to be sure.
});

, , , .

+6

BGerrissen, . , , :

$(document).ready(function () {  
  $('#root').keypress(function(e) { 
    if (e.keyCode == '13') { 
      e.preventDefault();//Stops the default action for the key pressed
      goButton(); 
      return false;//extra caution, may not be necessary
    } 
  });
});
+3

false if ( goButton()).

. : jQuery

0

All Articles