jquery禁用表单提交输入 [英] jquery disable form submit on enter

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

问题描述

  $('form')。

在我的页面中有以下javascript,看起来没有工作。 (e.keyCode == 13){
e.preventDefault();
return false;
}
});

我想禁用在输入时提交表单,或者更好地调用我的ajax表单提交。任何一种解决方案都是可以接受的,但是我上面包含的代码并不会阻止表单提交。

解决方案

如果 keyCode 未被捕获,请捕获其中

 <$ ('keyup keypress',function(e){
var keyCode = e.keyCode || e.which;
if(keyCode === 13){
e.preventDefault();
return false;
}
});

编辑:错过了,最好使用 keyup bind()更改为 code> on()


I have the following javascript in my page which does not seem to be working.

$('form').bind("keypress", function(e) {
  if (e.keyCode == 13) {               
    e.preventDefault();
    return false;
  }
});

I'd like to disable submitting the form on enter, or better yet, to call my ajax form submit. Either solution is acceptable but the code I'm including above does not prevent the form from submitting.

解决方案

If keyCode is not caught, catch which:

$('#formid').on('keyup keypress', function(e) {
  var keyCode = e.keyCode || e.which;
  if (keyCode === 13) { 
    e.preventDefault();
    return false;
  }
});

EDIT: missed it, it's better to use keyup instead of keypress

EDIT 2: As in some newer versions of Firefox the form submission is not prevented, it's safer to add the keypress event to the form as well. Also it doesn't work (anymore?) by just binding the event to the form "name" but only to the form id. Therefore I made this more obvious by changing the code example appropriately.

EDIT 3: Changed bind() to on()

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

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