HTML5:在任何文本输入中按ENTER之后,如何提交表单?
标题说明自己...我有几个形式,其中一种只是单个文本输入形式,另一个是由两个文本输入组成的。我不希望其中任何一个中的任何提交按钮,我希望每种表单在用户按任何文本输入处按ENTER按钮时提交:
- 仅一个输入组成的表单每次用户按下用户按下输入按钮 - 完美!
- 由两个文本输入组成的第二种形式并非以这种方式行为,当用户按两个输入处按ENTER按钮时,它不会提交。
有没有一种方法可以使形式具有多个文本输入以这种方式行事并避免在其中放上提交按钮?
The title speaks for itself... I have a couple of forms, one of them is just a single text input form and the other one is composed by two text inputs. I do not want any submit button in any of them, I want each form to submit whenever the user presses the ENTER button at any text input:
- The form composed by just one input submits everytime the user presses the ENTER button - Perfect!
- The second form composed by two text inputs does not behave this way, it does not submit when the user presses the ENTER button at any of both inputs.
Is there a way to make a form with more than one text input behave this way and avoid having a submit button in it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试在
< form>/form>
标签测试的标签之间添加它,并在Firefox和Chrome上工作。如果您的表格中有提交输入类型,则无论是否可见, Enter 都应自动提交。
实际上,我自己以登录形式使用它,尽管在用户名字段中,搬到下一个字段比提交更有意义。以防万一您有类似的用例,这是我使用的代码(需要jQuery)
请注意,如果用户选择浏览器自动完成用户名,请按 Enter ,它仍然移至下一个字段而不是选择它。没有时间来调试此问题,但是如果有人能弄清楚如何修复它,那就太酷了。
Try adding this between the
<form></form>
tagsTested it and it works on Firefox and Chrome. If you have a submit input type in the form, enter should automatically submit it, regardless of whether it's visible or not.
I am actually using this myself in a login form, though in the username field, it makes more sense to move to the next field than to submit. Just in case you have a similar use case, here's the code I used (requires jQuery)
Note that there is a slight bug though -- if the user selects a browser autocomplete username and presses enter, it still moves to the next field instead of selecting it. Didn't have time to debug this, but if someone can figure out how to fix it, that would be cool.
我一直在寻找解决此问题的解决方案,并希望根据这里的许多帖子分享我的解决方案。 (在现代铬/firefox/ie上进行测试)
因此,仅使用JavaScript使用javaScript,如果按钮
提交
按下。之后,它清除/重置表格,这是一件好事。希望它有帮助。
I was looking for a solution to this problem and want to share my solution, based on many posts over here. (Tested on modern Chrome/Firefox/IE)
So, using only Javascript the follow code submits the form if ENTER key is pressed on any field or if the button
Submit
is pressed. After that it clear/reset the form, which is a nice thing to do.Hope it helps.
确保您的输入在“表单”元素内部,并将“表单”元素作为“ action”属性。
Be sure that your inputs are inside "form" element and give the "form" element an "action" attribute.
您将必须查找Enter键按下。这篇文章显示了如何做到这一点。
输入javascript中的键按事件
You will have to look for the Enter key press. This post here shows how to do that.
Enter key press event in JavaScript
您可以使用下面的代码在按Enter后提交表格。
You can use the code below to submit a form after pressing Enter.