禁用页面上的输入键,但不在textarea中 [英] disable enter key on page, but NOT in textarea

查看:113
本文介绍了禁用页面上的输入键,但不在textarea中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

发现这个脚本:

Found this script:



function stopRKey(evt) {
  var evt = (evt) ? evt : ((event) ? event : null);
  var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
  if ((evt.keyCode == 13) && (node.type=="text"))  {return false;}
}

document.onkeypress = stopRKey;


只有问题,它也会停止在textarea中使用的输入密钥。这是一个麻烦。

Only issue, it also stops enter key being used in textarea. Which is a hassle.

我玩过使用:

onkeypress =return handleEnter(this,event)

但是我们的表单非常复杂,我正在寻找一种更干净的方式来处理事情。

But our forms are extremely complex, and I am looking for a cleaner way of doing things.

推荐答案

您需要检查 nodeName tagName ,如下所示:

You need to check the nodeName or tagName of the event target here, like this:

if (evt.keyCode == 13 && node.nodeName != "TEXTAREA") { return false; }






我注意到这个被接受后,你是已经使用jQuery,你可以用上面的代码替换上面所有的代码:


I noticed after this was accepted that you are already using jQuery, you can just replace all your code above with this:

$(document).keypress(function (e) {
  if(e.which == 13 && e.target.nodeName != "TEXTAREA") return false;
});

这篇关于禁用页面上的输入键,但不在textarea中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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