使用 jQuery 在“Enter"上提交表单? [英] Submitting a form on 'Enter' with jQuery?

查看:31
本文介绍了使用 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
  }
});

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

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

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

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

这篇关于使用 jQuery 在“Enter"上提交表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆