使用jQuery在'输入'提交表单? [英] Submitting a form on 'Enter' with jQuery?

查看:91
本文介绍了使用jQuery在'输入'提交表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用HTML / jQuery的AIR项目中,我有一个bog标准登录表单 - 一个电子邮件文本字段,一个密码字段和一个提交按钮。当我在表单上按Enter时,整个表单的内容将消失,但表单未提交。有人知道这是否是一个Webkit问题(Adobe AIR使用Webkit for HTML),或者我已经整理了一些东西?



我试过了:

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

但是,这既不能阻止清除行为,也不会提交表单。没有与表格相关的行动 - 这可能是问题吗?我可以在动作中放入一个javascript函数吗?

解决方案

  $('。input' ).keypress(function(e){
if(e.which == 13){
$('form#login')。submit();
return false; //< ; ----添加这行
}
});

注意:您已接受bendewey的答案,但它对e.preventDefault()的描述不正确。看看这个stackoverflow答案:
event.preventDefault()与返回false

基本上,return false与调用 e.preventDefault e.stopPropagation()


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?

I tried:

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

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

NOTE: You accepted bendewey's answer, but it is incorrect with its description of e.preventDefault(). Check out this stackoverflow answer: event.preventDefault() vs. return false

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

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

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