使用 jQuery 在“Enter"上提交表单?

问题描述:

我有一个沼泽标准登录表单 - 一个电子邮件文本字段、一个密码字段和一个使用 HTML/jQuery 的 AIR 项目上的提交按钮.当我在表单上按 Enter 键时,整个表单的内容都消失了,但表单并未提交.有谁知道这是 Webkit 问题(Adobe AIR 将 Webkit 用于 HTML),还是我搞砸了?

I have a bog-standard login form - an email text field, a password field and a submit button on an AIR project that's using HTML/jQuery. When I hit Enter on the form, the entire form's contents vanish, but the form isn't submitted. Does anyone know if this is a Webkit issue (Adobe AIR uses Webkit for HTML), or if I've bunged things up?

我试过:

$('.input').keypress(function (e) {
  if (e.which == 13) {
    $('form#login').submit();
  }
});

但这既没有停止清算行为,也没有提交表格.没有与表单相关的操作 - 这可能是问题吗?我可以在动作中加入一个 javascript 函数吗?

But that neither stopped the clearing behavior, or submitted the form. There's no action associated with the form - could that be the issue? Can I put a javascript function in the action?

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

看看这个*答案:event.preventDefault() vs. return false

Check out this * answer: event.preventDefault() vs. return false

本质上,return false"与调用 e.preventDefaulte.stopPropagation() 相同.

Essentially, "return false" is the same as calling e.preventDefault and e.stopPropagation().