嗯,我试图通过按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浏览器中不会显示该按钮。然而,我仍然不喜欢这个解决方案,因为很难知道它是否适用于所有平台和所有浏览器。

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


当前回答

只需要将hidden属性设置为true:

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

其他回答

而不是hack你目前使用隐藏按钮,这将是更简单的设置可见性:折叠;在样式属性中。但是,我仍然建议使用一些简单的Javascript来提交表单。据我所知,现在对这些事情的支持无处不在。

使用以下代码,这解决了我在所有3个浏览器(FF, IE和Chrome)的问题:

<input  type="submit" name="update" value=" Apply " 
    style="position: absolute; height: 0px; width: 0px; border: none; padding: 0px;"
    hidefocus="true" tabindex="-1"/>

将上述行添加为代码中的第一行,并使用适当的name和value值。

HTML5的解决方案

<input type="submit" hidden />

这是我的解决方案,在Chrome, Firefox 6和IE7+测试:

.hidden{
    height: 1px;
    width: 1px;
    position: absolute;
    z-index: -100;
}

我使用了很多UI框架。它们中的许多都有一个内置的类,您可以使用它来可视化地隐藏内容。

引导

<input type="submit" class="sr-only" tabindex="-1">

角材料

<input type="submit" class="cdk-visually-hidden" tabindex="-1">

创建这些框架的天才们将这些风格定义如下:

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

.cdk-visually-hidden {
    border: 0;
    clip: rect(0 0 0 0);
    height: 1px;
    margin: -1px;
    overflow: hidden;
    padding: 0;
    position: absolute;
    width: 1px;
    outline: 0;
    -webkit-appearance: none;
    -moz-appearance: none;
}