在没有提交按钮的情况下,按下回车键即可提交表单 [英] Getting a form to submit when the enter key is pressed when there is no submit button

查看:78
本文介绍了在没有提交按钮的情况下,按下回车键即可提交表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个没有提交按钮的表单,如果在用户按下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!

这篇关于在没有提交按钮的情况下,按下回车键即可提交表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆