好吧,我想通过按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>
Run Code Online (Sandbox Code Playgroud)
哪个效果很好.当用户按下Enter键时,提交按钮会起作用,并且该按钮不会显示在Firefox,IE,Safari,Opera和Chrome中.但是,我仍然不喜欢这个解决方案,因为很难知道它是否适用于所有浏览器的所有平台.
有谁能建议更好的方法?或者这是否与它一样好?
Ate*_*ral 231
尝试:
<input type="submit" style="position: absolute; left: -9999px"/>
Run Code Online (Sandbox Code Playgroud)
这将按钮向左推出,离开屏幕.这样做的好处是,当CSS被禁用时,你会得到优雅的降级.
更新 - IE7的解决方法
正如Bryan Downing +所建议的,tabindex
以防止标签到达此按钮(由Ates Goral提供):
<input type="submit"
style="position: absolute; left: -9999px; width: 1px; height: 1px;"
tabindex="-1" />
Run Code Online (Sandbox Code Playgroud)
str*_*ger 94
我认为你应该去Javascript路线,或者至少我会:
<script type="text/javascript">
// Using jQuery.
$(function() {
$('form').each(function() {
$(this).find('input').keypress(function(e) {
// Enter pressed?
if(e.which == 10 || e.which == 13) {
this.form.submit();
}
});
$(this).find('input[type=submit]').hide();
});
});
</script>
<form name="loginBox" target="#here" method="post">
<input name="username" type="text" /><br />
<input name="password" type="password" />
<input type="submit" />
</form>
Run Code Online (Sandbox Code Playgroud)
and*_*dyk 76
你试过这个吗?
<input type="submit" style="visibility: hidden;" />
Run Code Online (Sandbox Code Playgroud)
由于大多数浏览器都理解visibility:hidden
并且它不能真正起作用display:none
,我猜它应该没问题.我自己没有真正测试过,所以CMIIW.
dam*_*ser 29
没有提交按钮的另一个解决方案
HTML
<form>
<input class="submit_on_enter" type="text" name="q" placeholder="Search...">
</form>
Run Code Online (Sandbox Code Playgroud)
jQuery的
$(document).ready(function() {
$('.submit_on_enter').keydown(function(event) {
// enter has keyCode = 13, change it if you want to use another button
if (event.keyCode == 13) {
this.form.submit();
return false;
}
});
});
Run Code Online (Sandbox Code Playgroud)
Pan*_*osh 15
对于将来查看此答案的任何人,HTML5都会为表单元素实现一个新属性hidden
,该属性将自动应用于display:none
您的元素.
例如
<input type="submit" hidden />
Run Code Online (Sandbox Code Playgroud)
小智 13
使用以下代码,这解决了我在所有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"/>
Run Code Online (Sandbox Code Playgroud)
将上面的行添加为代码中的第一行,并使用适当的name和value值.
您可以visibility: collapse;
在style属性中设置,而不是当前用来隐藏按钮的hack .但是,我仍然建议使用一些简单的Javascript来提交表单.据我所知,现在对这类事物的支持无处不在.
只需将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>
Run Code Online (Sandbox Code Playgroud)
最优雅的方法是保留提交按钮,但将其边框,填充和字体大小设置为0.
这将使按钮尺寸为0x0.
<input type="submit" style="border:0; padding:0; font-size:0">
Run Code Online (Sandbox Code Playgroud)
您可以自己尝试,并通过设置元素的轮廓,您将看到一个点,这是"围绕"0x0 px元素的外边框.
不需要能见度:隐藏,但如果它让你在晚上睡觉,你也可以把它扔进混合物中.
<input type="submit" style="display:none;"/>
Run Code Online (Sandbox Code Playgroud)
这很好用,它是您想要实现的最明确的版本。
请注意,有之间的差异display:none
和visibility:hidden
其他表单元素。
我使用了一堆 UI 框架。它们中的许多都有一个内置类,您可以使用它来直观地隐藏事物。
<input type="submit" class="sr-only" tabindex="-1">
Run Code Online (Sandbox Code Playgroud)
<input type="submit" class="cdk-visually-hidden" tabindex="-1">
Run Code Online (Sandbox Code Playgroud)
创建这些框架的天才将这些风格定义如下:
.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;
}
Run Code Online (Sandbox Code Playgroud)
小智 5
这是我的解决方案,在 Chrome、Firefox 6 和 IE7+ 中测试过:
.hidden{
height: 1px;
width: 1px;
position: absolute;
z-index: -100;
}
Run Code Online (Sandbox Code Playgroud)
小智 5
最简单的方法
<input type="submit" style="width:0px; height:0px; opacity:0;"/>
Run Code Online (Sandbox Code Playgroud)
小智 5
如果提交按钮不可见,IE不允许按ENTER键进行表单提交,并且表单有多个字段.通过提供1x1像素透明图像作为提交按钮,给它想要的东西.当然它会占用布局的一个像素,但看看你需要做些什么来隐藏它.
<input type="image" src="img/1x1trans.gif"/>
Run Code Online (Sandbox Code Playgroud)