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

html HTML

阿飞

2020-03-24

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

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

第3249篇《通过按Enter键提交表单(不带提交按钮)》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

15个回答
蛋蛋逆天 2020.03.24

你也可以尝试这个

<INPUT TYPE="image" SRC="0piximage.gif" HEIGHT="0" WIDTH="0" BORDER="0">

您可以添加宽度/高度= 0像素的图像

理查德Eva 2020.03.24

我使用大量的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;
}
西门 2020.03.24

对于那些有IE问题的人,也对其他人。

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

最简单的方法

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

HTML5解决方案

<input type="submit" hidden />
斯丁前端 2020.03.24
<input type="submit" style="display:none;"/>

效果很好,它是您要实现的最明确的版本。

请注意,有之间的差异display:nonevisibility:hidden其他表单元素。

小小神奇 2020.03.24

这是我的解决方案,已在Chrome,Firefox 6和IE7 +中进行了测试:

.hidden{
    height: 1px;
    width: 1px;
    position: absolute;
    z-index: -100;
}
神乐Mandy米亚 2020.03.24

只需将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>
前端村村 2020.03.24

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

这将使按钮尺寸为0x0。

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

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

不需要可见性:隐藏,但如果它使您晚上入睡,也可以将其放入混合中。

JS小提琴

西里Near 2020.03.24

如果看不见“提交”按钮,并且表单具有多个字段,则IE不允许按ENTER键提交表单。提供1x1像素的透明图像作为提交按钮,以提供所需的内容。当然,它会占用布局的一个像素,但是请注意隐藏它所要做的事情。

<input type="image" src="img/1x1trans.gif"/>
Tony西门 2020.03.24

代替您当前用来隐藏按钮的技巧visibility: collapse;,在style属性中进行设置要简单得多但是,我仍然建议使用一些简单的Javascript提交表单。据我了解,如今对此类事物的支持无处不在。

樱小胖Mandy 2020.03.24

对于以后希望看到此答案的任何人,HTML5都会为表单元素实现一个新属性hidden,该属性会自动应用于display:none您的元素。

例如

<input type="submit" hidden />
村村 2020.03.24

使用以下代码,可以解决所有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"/>

将上面的行作为代码中的第一行添加名称和值的适当值。

2020.03.24

你尝试过这个吗?

<input type="submit" style="visibility: hidden;" />

由于大多数浏览器都能理解visibility:hidden,但实际上并不能像一样正常工作display:none,但我猜应该没问题。CMIIW自己还没有真正测试过。

小胖Gil 2020.03.24

尝试:

<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" />

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android