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

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


当前回答

我认为你应该走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>

其他回答

使用以下代码,这解决了我在所有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值。

你也可以试试这个

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

你可以包含一个宽度/高度= 0 px的图像

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

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

2022年更新:用这个代替

<input type="submit" hidden />

Notice - Outdated answer
Please do not use position: absolute in the year 2021+. It's recommended to use the hidden attribute instead. Otherwise, look down below and pick a better, more modern, answer.

Try:

<input type="submit" style="position: absolute; left: -9999px"/>

这会把按钮推到屏幕的左边。这样做的好处是,当CSS被禁用时,你会得到优雅的降级。

更新- IE7的解决方案

正如Bryan Downing +使用tabindex来防止tab到达这个按钮(由Ates Goral)所建议的:

<input type="submit" 
       style="position: absolute; left: -9999px; width: 1px; height: 1px;"
       tabindex="-1" />

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

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