防止按 Enter 键时提交表单
我们怎样才能防止按下回车键时提交表单。
实际上我有一个文本框,在该文本框中输入一个值并单击输入时,textbox2 将获得焦点。
默认情况下,单击输入按钮后将提交表单。所以我无法得到输出。我在提交按钮的 onclick 事件上写了 return false 然后它就起作用了。 所以我想知道如何防止按 Enter 时提交表单。
how can we prevent a form from being submitted while pressing enter key.
Actually I have a text box and on entering a value on that textbox and clicking enter the textbox2 will get focused.
By default on clicking the enter button the form will get submitted. So I can not get the out put. I wrote return false on onclick event of the submit button then it works.
So I would like to know how can we prevent a form submission while pressing enter.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在不查找实际代码的情况下,以下是我如何做到这一点背后的基本理论:
在文档上为按下的键调用函数定义一个事件处理程序。
如果触发事件的按键是 Enter/Return 键,则
返回 false
,否则返回 true
。编辑:看起来您已经找到了答案(以及一些代码来做到这一点),所以我的答案现在有些不必要。
Without looking up the actual code, here's the basic theory behind how I'd do it:
Define an event handler on the document for a key being pressed to call a function.
If the key that triggered the event is the enter/return key,
return false
, otherwisereturn true
.EDIT: Looks like you already found the answer (as well as some code to do it), so my answer is somewhat unnecessary now.
编写“onKeyPress”形式,如“
{ var key; if(window.event) key = window.event.keyCode; else key = e.which; //firefox return (key != 13); }< /code> ” 会起作用。
Writing 'onKeyPress' of the form like "
{ var key; if(window.event) key = window.event.keyCode; else key = e.which; //firefox return (key != 13); }
" will work.我所做的是将
onsubmit="return false;"
添加到what i do is add
onsubmit="return false;"
to the<form>
tag.this will also disable the submit button however to create a working submit button use
这应该有效
This should work