输入按钮作为提交
我需要使用 Enter 键提交表单,但是,我必须使用 BUTTON 而不是 SUBMIT 作为类型,以便页面不刷新。如何让我的按钮充当“提交”并在有人按下回车键时执行?
<form>
<input type=text ...>
<input type=button ...>
</form>
我发现的很多关于这方面的信息都提到了 Netscape/IE/很多过时的材料。
这是我的 HTML 输出,我希望隐藏提交按钮并使用 ENTER:
I need to have a form submitted using the enter key, however, I have to use a BUTTON instead of SUBMIT as the type in order for the page to not refresh. How can I get my BUTTON to act as a SUBMIT and be executed whenever someone pushes their enter key?
<form>
<input type=text ...>
<input type=button ...>
</form>
A lot of the information I found about this mentions Netscape/IE/lots of outdated material.
This is my HTML output, I'm looking to hide the submit button and use ENTER:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
启用 Javascript
应该可以工作
with Javascript enabled
should work
Nah。使用普通的
submit
按钮来刷新页面。 (理想情况下,为了可访问性,让它发挥作用!)然后添加渐进增强,以在 JS 可用时用更流畅的操作替换表单的提交操作。在这种情况下,请使用return false
(或 DOM 2 事件模型中的event.preventDefault()
)来停止表单提交。捕获表单的
submit
事件通常比尝试获取按钮上的click
更好,因为它始终会在表单正常提交时触发,包括按 Enter 键时触发。单击
表单中的第一个提交按钮通常会在按下 Enter 按键时触发,但也有一些情况(取决于表单中的控件数量以及浏览器) )而这种情况不会发生,因此您可能最终无法实际提交表单。Nah. Use a normal
submit
button that refreshes the page. (And ideally, for accessibility, make it work!) Then add progressive enhancement to replace the submission action of the form with something smoother when JS is available. Usereturn false
(orevent.preventDefault()
in the DOM 2 Events model) to stop the form submitting in this case.Catching the
submit
event of a form is generally better than trying to pick upclick
on buttons, because it will always fire when the form would normally be submitted, including on Enter keypresses.click
on the first submit button in a form will usually be fired on an Enter keypress, but there are cases (depending on number of controls in the form and what browser it is) where it doesn't happen and so you can end up falling through to actually submitting the form.正如其他人所说,你必须使用 Javascript。我推荐JQuery框架。
但我不明白刷新是什么?
正常的方式是您点击提交,您的表单将通过请求发送到服务器。
服务器处理数据并返回响应(HTML/JSon..etc),该响应通常会重定向到结果页面(以避免刷新时重新发布的著名警告)。
现在,如果您的表单只是较大页面的一小部分,您可能需要使用 ajax 来发布小表单,然后获取结果并更新您的 DOM。
综上所述,没有什么可以阻止您使用按钮的提交类型,这实际上是使您的 Enter 键默认执行此操作的最佳方法。您所要做的就是使用 Jquery 并拦截表单的提交并进行 ajax 调用,而不是采用正常的方式。
您会发现大量使用 JQuery 的示例,因为它可能是最常用的 JavaScript 框架。
希望有帮助
as other said, you have to use Javascript. I recommend JQuery framework.
But i don't understand the refresh thing?
Normal way is you hit submit and your form will be sent over a request to the server.
Server process the data and return a response (HTML/JSon..etc) this response will normally be redirect to a result page (to avoid the famous warning about re-post on refresh).
Now if your form is only a little piece of a bigger page, you might want to use ajax to post the little form and then take the result and update your DOM.
All this said, nothing prevent you to use submit type for the button, it is actually the best way to make your enter key defaut to this action. All you have to do is to use Jquery and intercept the submit of your form and make an ajax call instead of going the normal way.
you will find plenty of example to use JQuery since its probably the most used javascript framework.
Hope it help