在没有提交按钮的情况下,按下回车键即可提交表单

在没有提交按钮的情况下,按下回车键即可提交表单

问题描述:

我有一个没有提交按钮的表单,如果在用户按下Enter键时仍然提交表单,那就太好了.我尝试添加一个隐藏的提交按钮,这使我在Safari,Firefox和Chrome浏览器中具有所需的行为,但是该表单仍未在IE中提交.有标准的方法吗?

I've got a form that has no submit button and it would be great if the form is still submitted when the user hits the enter key. I've tried adding a hidden submit button, which gives me the desired behavior in Safari, Firefox and Chrome, but the form is still not submitted in IE. Is there a standard way of doing this?

经过测试/跨浏览器:

function submitOnEnter(e) {
    var theEvent = e || window.event;
    if(theEvent.keyCode == 13) {
        this.submit;
    }
    return true;
}
document.getElementById("myForm").onkeypress = function(e) { return submitOnEnter(e); }

<form id="myForm">
<input type="text"/>
...
</form>

如果没有提交"按钮,则在无法使用javascript的情况下,表单将严重恶化!

If there is no submit button, the form will degrade miserably if javascript is not available!