admin管理员组

文章数量:1300026

$(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.

$(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.

Share Improve this question edited Feb 14, 2013 at 6:21 Samuel Liew 79.1k111 gold badges168 silver badges303 bronze badges asked Feb 14, 2013 at 5:36 RajasekharRajasekhar 2,4553 gold badges29 silver badges50 bronze badges 2
  • In IE, jQuery will fire keydown(), not keypress() for the arrow keys, because they are considered 'special' keys. – sasi Commented Feb 14, 2013 at 5:44
  • possible duplicate :stackoverflow./questions/1427912/… and stackoverflow./questions/3546140/… – depz123 Commented Feb 14, 2013 at 5:44
Add a ment  | 

4 Answers 4

Reset to default 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;
        }
    });
});

try

$('.inviteClass').keypress(function (e) {
  c = e.which ? e.which : e.keyCode;
  if (c == 13) {
    doPost();
    e.preventDefault();
    return false;    //<---- Add this line
  }
});

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();
        }
    });
});

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

本文标签: javascriptquotEnterquot Keypress to Submit the Form is not working in IE 910Stack Overflow