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

Categories

0 votes
412 views
in Technique[技术] by (71.8m points)

javascript - 如何防止ENTER键提交Web表单?(How to prevent ENTER keypress to submit a web form?)

如何防止ENTER键在基于Web的应用程序中提交表单?

  ask by user67722 translate from so

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

1 Answer

0 votes
by (71.8m points)

[ revision 2012, no inline handler, preserve textarea enter handling]

([2012年修订版 ,没有内联处理程序,保留textarea输入处理内容])

function checkEnter(e){
 e = e || event;
 var txtArea = /textarea/i.test((e.target || e.srcElement).tagName);
 return txtArea || (e.keyCode || e.which || e.charCode || 0) !== 13;
}

Now you can define a keypress handler on the form:

(现在,您可以在表单上定义按键处理程序:)


<form [...] onkeypress="return checkEnter(event)">

( <form [...] onkeypress =“ return checkEnter(event)”> )

document.querySelector('form').onkeypress = checkEnter;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...