通过按Enter键提交表单,而不提交提交按钮(Submitting a form by pressing enter without a submit button)

编程入门 行业动态 更新时间:2024-10-27 05:28:13
通过按Enter键提交表单,而不提交提交按钮(Submitting a form by pressing enter without a submit button)

那么我正在尝试通过按Enter键提交表单,但不显示提交按钮。 如果可能,我不想进入JavaScript,因为我希望所有浏览器都可以使用所有浏览器(我唯一知道的JS方式是使用事件)。

现在的形式如下:

<form name="loginBox" target="#here" method="post"> <input name="username" type="text" /><br /> <input name="password" type="password" /> <input type="submit" style="height: 0px; width: 0px; border: none; padding: 0px;" hidefocus="true" /> </form>

哪个工作相当不错 当用户按下Enter键时,提交按钮可以工作,该按钮不会显示在Firefox,IE,Safari,Opera和Chrome中。 但是,我仍然不喜欢这个解决方案,因为它很难知道它是否能在所有浏览器的所有平台上运行。

任何人都可以提出更好的方法吗 或者这是关于它得到的好吗?

Well I am trying to submit a form by pressing enter but not displaying a submit button. I don't want to get into JavaScript if possible since I want everything to work on all browsers (the only JS way I know is with events).

Right now the form looks like this:

<form name="loginBox" target="#here" method="post"> <input name="username" type="text" /><br /> <input name="password" type="password" /> <input type="submit" style="height: 0px; width: 0px; border: none; padding: 0px;" hidefocus="true" /> </form>

Which works pretty well. The submit button works when the user presses enter, and the button doesn't show in Firefox, IE, Safari, Opera and Chrome. However, I still don't like the solution since it is hard to know whether it will work on all platforms with all browsers.

Can anyone suggest a better method? Or is this about as good as it gets?

最满意答案

尝试:

<input type="submit" style="position: absolute; left: -9999px"/>

这将把按钮waaay向左​​推出屏幕。 这样做的好处是,当CSS被禁用时,你会得到优雅的降级。

更新 - IE7的解决方法

如Bryan Downing +与tabindex建议,以防止tab达到此按钮(由Ates Goral):

<input type="submit" style="position: absolute; left: -9999px; width: 1px; height: 1px;" tabindex="-1" />

Try:

<input type="submit" style="position: absolute; left: -9999px"/>

That will push the button waaay to the left, out of the screen. The nice thing with this is, you'd get graceful degradation when CSS is disabled.

Update - Workaround for IE7

As suggested by Bryan Downing + with tabindex to prevent tab reach this button (by Ates Goral):

<input type="submit" style="position: absolute; left: -9999px; width: 1px; height: 1px;" tabindex="-1" />

更多推荐

submit,button,电脑培训,计算机培训,IT培训"/> <meta name="description&quo

本文发布于:2023-07-29 14:15:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1316809.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而不   表单   按钮   Submitting   Enter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!