防止“输入”从提交表单,但允许它在textarea字段(jQuery) [英] Preventing "Enter" from submitting form, but allow it on textarea fields (jQuery)

查看:243
本文介绍了防止“输入”从提交表单,但允许它在textarea字段(jQuery)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  $(window).keydown(function(event){
if(event.keyCode == 13){
event.preventDefault();
返回false;
}
});

以上是我得到的代码,它有效地杀死了作为表单提交者的输入系统,这正是我想要的。但是,在textarea标签上也禁用了输入键 - 用户应该能够进入该输入键以进入下一行。那么是否有办法修改上面的代码来检测输入是否来自textarea标签,它不会运行event.preventDefault();线?

我在整个网站中有很多形式 - 必须单独配置它们将是一场噩梦,并且可能没有意义 - 这应该是一种通用的方式。上述代码在网站的每个页面上运行,以防止通过点击输入而意外提交。 在此处输入代码

解决方案

我更喜欢 keyup event ...使用 event.target 属性

 ($ event_which == 13)&&($(event.target)[0]!= $($($))$($) textarea)[0])){
event.preventDefault();
return false;
}
});



demo


$(window).keydown(function(event){
    if(event.keyCode == 13) {
      event.preventDefault();
      return false;
    }
  });

The above is the code I got which effectively kills the "enter" key as a form submitter throughout the system, which is exactly what I want. However, the enter key is also disabled on textarea tags - which the users should be able to hit enter on in order to go to the next rows. So is there a way to modify the above code to detect IF the enter is coming from within a textarea tag, it doesn't run the event.preventDefault(); line?

I have so many forms throughout the site - having to configure them individually would be a nightmare, and probably doesn't make sense - there's gotta be a universal way. The above code runs on every single page of the site to prevent accidental submits by hitting "enter". enter code here

解决方案

i would prefer the keyup event ... use the event.target property

$(window).keydown(function(event){
    if((event.which== 13) && ($(event.target)[0]!=$("textarea")[0])) {
      event.preventDefault();
      return false;
    }
  });

demo

这篇关于防止“输入”从提交表单,但允许它在textarea字段(jQuery)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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