0
$(function(){
    $('.inviteClass').keypress(function() {
        if(event.keyCode=='13') {
            doPost();
        }
});

Here I have one small requirement. Pressing keyboard Enter to submit the form and it is working fine in FireFox and Chrome, as well as IE 7 and 8, but it is not working in IE9 and IE 10.

Please help me.

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
Rajasekhar
  • 2,215
  • 3
  • 23
  • 38
  • In IE, jQuery will fire keydown(), not keypress() for the arrow keys, because they are considered 'special' keys. – sasi Feb 14 '13 at 05:44
  • possible duplicate :http://stackoverflow.com/questions/1427912/in-ie8-enter-key-in-a-form-does-not-work and http://stackoverflow.com/questions/3546140/enter-does-not-submit-form-in-ie-because-of-hidden-button – depz123 Feb 14 '13 at 05:44

4 Answers4

8

Points to note:

  1. You are missing a closing bracket.
  2. Also, change the selector to window
  3. Use .on() function
  4. Use the .which property of event. See jQuery documentation
  5. The keycode is an integer - remove the quotes
  6. Add a return false; to stop the event from bubbling to the form (and possibly submitting the form twice). See Submitting a form on 'Enter' with jQuery?

Final code:

$(function() {
    $(window).on('keydown', function(event) {
        if(event.which == 13) {
            doPost();
            return false;
        }
    });
});
Community
  • 1
  • 1
Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
  • 1
    the `keypress` function is a shortcut for `.on( "keypress", handler )`, so point `3` is invalid. See also: https://api.jquery.com/keypress/. – Jacob van Lingen Sep 22 '16 at 09:14
1

try

$('.inviteClass').keypress(function (e) {
  c = e.which ? e.which : e.keyCode;
  if (c == 13) {
    doPost();
    e.preventDefault();
    return false;    //<---- Add this line
  }
});
Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110
0

you must use jQuery's event.which, also change '13' to 13 (a closing bracket was also missing):

$(function(){
    $('.inviteClass').keypress(function(event) {
        if(event.which == 13) {
            doPost();
        }
    });
});
alijsh
  • 842
  • 7
  • 7
-1

Please Try to use keydown event and also pass the event object in the function like this

$(function(){$('.inviteClass').keydown(function(event){if(event.keyCode=='13'){doPost();}});

or

$(function(){$('.inviteClass').keypress(function(event){if(event.keyCode=='13'){doPost();}});

Hope this will help you

Thanks

Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110
Madhu Magar
  • 435
  • 4
  • 18