嗯,我试图通过按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>

这很有效。当用户按下回车键时,提交按钮就会正常工作,但在Firefox、IE、Safari、Opera和Chrome浏览器中不会显示该按钮。然而,我仍然不喜欢这个解决方案,因为很难知道它是否适用于所有平台和所有浏览器。

谁能提出一个更好的方法?还是说这已经是最好的结果了?


当前回答

HTML5的解决方案

<input type="submit" hidden />

其他回答

最简单的方法

<input type="submit" style="width:0px; height:0px; opacity:0;"/>

最优雅的方法是保留提交按钮,但将其边框、填充和字体大小设置为0。

这将使按钮尺寸为0x0。

<input type="submit" style="border:0; padding:0; font-size:0">

您可以自己尝试,通过设置元素的轮廓,您将看到一个点,这是“包围”0x0 px元素的外部边界。

不需要可见性:隐藏,但如果它让你在晚上睡觉,你也可以把它放在一起。

JS小提琴

下面是对我有效的代码,相信它会对你有帮助

<form name="loginBox" target="#here" method="post">
  <input name="username" type="text" /><br />
  <input name="password" type="password" />
  <input type="submit" />
</form>

<script type="text/javascript">
  $(function () {
    $("form").each(function () {
      $(this)
        .find("input")
        .keypress(function (e) {
          if (e.which == 10 || e.which == 13) {
            this.form.submit();
          }
        });
      $(this).find("input[type=submit]").hide();
    });
  });
</script>

HTML5的解决方案

<input type="submit" hidden />

对于IE有问题的人,也适用于其他人。

{
    float: left;
    width: 1px;
    height: 1px;
    background-color: transparent;
    border: none;
}