Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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;


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

I have toyed with using: onkeypress="return handleEnter(this, event)"

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
394 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...