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

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


当前回答

下面是对我有效的代码,相信它会对你有帮助

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

其他回答

<input type="submit" style="display:none;"/>

这很好,而且它是你想要达到的最明确的版本。

注意,对于其他表单元素,display:none和visibility:hidden是有区别的。

我把它添加到document ready的函数中。如果表单上没有提交按钮(我所有的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);
    });
}
input.on('keypress', function(event) {
    if ( event.which === 13 ) {
        form.submit();
        return false;
    }
});

使用以下代码,这解决了我在所有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="submit" style="visibility: hidden;" />

因为大多数浏览器都理解visibility:hidden,它并不像display:none那样工作,所以我猜它应该没问题。我还没有亲自测试过,所以CMIIW。