好吧,我正在尝试通过按 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 中不显示。但是,我仍然不喜欢该解决方案,因为很难知道它是否适用于所有浏览器的所有平台。
任何人都可以提出更好的方法吗?或者这和它一样好?
2022 年更新:改用这个
<input type="submit" hidden />
注意 - 过时的答案请不要使用 position: absolute in the year 2021+。建议改用 hidden 属性。否则,请往下看,然后选择一个更好、更现代的答案。
尝试:
<input type="submit" style="position: absolute; left: -9999px"/>
这会将按钮向左推到屏幕外。这样做的好处是,当 CSS 被禁用时,你会得到优雅的降级。
更新 - IE7 的解决方法
正如 Bryan Downing + with tabindex
所建议的那样,以防止标签到达此按钮(由 Ates Goral 提供):
<input type="submit"
style="position: absolute; left: -9999px; width: 1px; height: 1px;"
tabindex="-1" />
我认为你应该走 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>
keypress
切换到 keydown
以获得更广泛的支持,但如果您希望支持 Chrome,还需要在 if
之前添加 e.preventDefault();
。
.on('keypress'...)
。 .on()
的文档看起来像是为您执行 .preventDefault()
调用。
你试过这个吗?
<input type="submit" style="visibility: hidden;" />
由于大多数浏览器都理解 visibility:hidden
并且它并不像 display:none
那样真正工作,但我猜它应该没问题。我自己还没有真正测试过,所以CMIIW。
visibility: hidden
只有一件事:正如您在 w3schools 中所读到的,visibity:none 仍然会影响布局。如果你想避免这个空白,绝对定位的解决方案对我来说似乎更好。
<input type="submit" style="visibility: hidden; position: absolute;" />
position: absolute; left: -100px; width: 1px; height: 1px;
没有提交按钮的另一种解决方案:
HTML
<form>
<input class="submit_on_enter" type="text" name="q" placeholder="Search...">
</form>
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;
}
});
});
对于将来查看此答案的任何人,HTML5 为表单元素实现了一个新属性 hidden
,它会自动将 display:none
应用于您的元素。
例如
<input type="submit" hidden />
使用以下代码,这解决了我在所有 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"/>
将上面的行添加为代码中的第一行,并带有适当的名称和值。
只需将 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 不同,在 style 属性中设置 visibility: collapse;
会简单得多。但是,我仍然建议使用一些简单的 Javascript 来提交表单。据我了解,如今对此类事物的支持无处不在。
visibility: collapse
解释为 visibility: hidden
,因此该按钮仍会导致白色区域:caniuse.com/mdn-css_properties_visibility_collapse
最优雅的方法是保留提交按钮,但将其边框、填充和字体大小设置为 0。
这将使按钮尺寸为 0x0。
<input type="submit" style="border:0; padding:0; font-size:0">
您可以自己尝试一下,通过为元素设置轮廓,您将看到一个点,它是“包围” 0x0 px 元素的外部边框。
不需要可见性:隐藏,但如果它让你晚上睡觉,你也可以把它混在一起。
HTML5解决方案
<input type="submit" hidden />
如果提交按钮不可见,并且表单有多个字段,IE 不允许按 ENTER 键提交表单。通过提供一个 1x1 像素的透明图像作为提交按钮,给它想要的东西。当然它会占用布局的一个像素,但是看看你必须做什么来隐藏它。
<input type="image" src="img/1x1trans.gif"/>
<input type="submit" style="display:none;"/>
这很好用,它是您想要实现的最明确的版本。
请注意,对于其他表单元素,display:none
和 visibility:hidden
之间存在差异。
我使用一堆 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;
}
这是我的解决方案,在 Chrome、Firefox 6 和 IE7+ 中进行了测试:
.hidden{
height: 1px;
width: 1px;
position: absolute;
z-index: -100;
}
最简单的方法
<input type="submit" style="width:0px; height:0px; opacity:0;"/>
input.on('keypress', function(event) {
if ( event.which === 13 ) {
form.submit();
return false;
}
});
if (event.key === "Enter")
来检查密钥是什么。
对于那些对 IE 有问题的人和其他人也是如此。
{
float: left;
width: 1px;
height: 1px;
background-color: transparent;
border: none;
}
float: left;
也一样,删除后,它会提交。
你也可以试试这个
<INPUT TYPE="image" SRC="0piximage.gif" HEIGHT="0" WIDTH="0" BORDER="0">
您可以包含宽度/高度 = 0 px 的图像
我将它添加到文档准备好的功能中。如果表单上没有提交按钮(我所有的 Jquery 对话框表单都没有提交按钮),请附加它。
$(document).ready(function (){
addHiddenSubmitButtonsSoICanHitEnter();
});
function addHiddenSubmitButtonsSoICanHitEnter(){
var hiddenSubmit = "<input type='submit' style='position: absolute; left: -9999px; width: 1px; height: 1px;' tabindex='-1'/>";
$("form").each(function(i,el){
if($(this).find(":submit").length==0)
$(this).append(hiddenSubmit);
});
}
这是对我有用的代码,肯定会对您有所帮助
<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>
不定期副业成功案例分享
position: absolute; width: 1px; height: 1px; left: -9999px;
tabindex="-1"