小编典典

通过按Enter键提交表单(不带提交按钮)

html

好吧,我试图通过按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中不显示。但是,我仍然不喜欢该解决方案,因为很难知道它是否可以在所有浏览器的所有平台上运行。

谁能提出更好的方法?还是说它尽可能地好?


阅读 544

收藏
2020-05-10

共1个答案

小编典典

尝试:

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

这会将waaay按钮推到屏幕的左侧。这样做的好处是,禁用CSS时,性能会下降。

更新-IE7的解决方法

正如布莱恩·唐宁(Bryan Downing +)所建议的,tabindex以防止该标签到达此按钮(由Ates Goral):

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