TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Pressing Enter should submit the form

5 点作者 jonascopenhagen大约 12 年前
There are too many forms out there where pressing Enter (when an input field is focused) does nothing. Pressing Enter should submit the form.<p>Normal HTML(5) forms work this way, but if your form is mostly managed through JS you don't necessarily get this default functionality.<p>If you don't actually use the &#60;form&#62; tag but instead just have an input field and a submit button, pressing Enter will do nothing. In that case there are two solutions:<p>1) Put the input fields inside a &#60;form&#62;. (If you need JS to control form submission, you can always include an onsubmit event handler that returns true or false depending on whether the user filled in the form correctly). Example: http://jsfiddle.net/LFkGZ/<p>2) Attach a keydown event to &#60;body&#62; so that when the user presses Enter you can submit the form. Example: http://jsfiddle.net/6r7gh/

3 条评论

ScottWhigham大约 12 年前
Except when it shouldn't: multi-line textboxes (such as those on a "Contact Us" or "Description" box) need the ability to let the user hit Enter without the form being submitted.
评论 #5455193 未加载
jonascopenhagen大约 12 年前
Clickable: <a href="http://jsfiddle.net/LFkGZ/" rel="nofollow">http://jsfiddle.net/LFkGZ/</a> <a href="http://jsfiddle.net/6r7gh/" rel="nofollow">http://jsfiddle.net/6r7gh/</a>
goofygrin大约 12 年前
Small forms... Sure. Long forms... Not so much.