如何在表单中输入提交按钮
我有一个用于登录我的网站的表格。我需要做到这一点,以便当用户按下回车键时,表单就会提交。我该怎么做?请提供代码。
<form id="login" action="myHome.php" method="POST">
<input type="text" name="email" id="email"/>
<br/>
<br/>
<input type="text" name="password" id="password"/>
</form>
I have a form for logging into my website. I need to make it so that when the user hits enter, the form submits. How can I do this? Please provide code.
<form id="login" action="myHome.php" method="POST">
<input type="text" name="email" id="email"/>
<br/>
<br/>
<input type="text" name="password" id="password"/>
</form>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要添加一个
并用 CSS 隐藏它,以便浏览器知道按下 Enter 时要触发什么,但仍然不显示按钮。为了可访问性和易用性,即使没有很多人使用它(输入更好),我也会显示该按钮。
You need to add an
<input type="submit">
and hide it with CSS so that the browser knows what to trigger when enter is pressed, yet still not show a button. For the sake of accessibility and ease of use, I'd show the button even if not that many people use it (enter is much nicer).添加一个处理程序到 on keydown 并检查 keycode == 13。使其提交如下所示的表单
add a handler to on keydown and check for keycode == 13. Make this submit the form like below
默认情况下应该会发生这种情况。根据我的经验,某些浏览器需要
字段,但通常这不是必需的。
This should happen by default. In my experience some browsers require an
<input type=submit>
field, but generally this is not a requirement.