Home  >  Article  >  Web Front-end  >  How to Prevent Clearing Login Forms on Enter Keypress in jQuery?

How to Prevent Clearing Login Forms on Enter Keypress in jQuery?

Barbara Streisand
Barbara StreisandOriginal
2024-10-25 11:35:30808browse

How to Prevent Clearing Login Forms on Enter Keypress in jQuery?

How to Submit a Form on 'Enter' with jQuery

When working with login forms in an AIR project using HTML/jQuery, you may encounter an issue where pressing Enter clears the form's content instead of submitting it. To resolve this, ensure that your code properly handles this event.

The following code should accomplish this:

$('.input').keypress(function (e) {
  if (e.which == 13) {
    $('form#login').submit();
    return false;    //<---- Add this line
  }
});

The key point here is adding the "return false" statement. This prevents the default action (clearing the form) from occurring while allowing the submit event to be triggered.

For more detailed information on this topic, refer to the stackoverflow answer on the subject of "event.preventDefault() vs. return false".

The above is the detailed content of How to Prevent Clearing Login Forms on Enter Keypress in jQuery?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn