How to make enter the submit button in a form

本小妞迷上赌 提交于 2019-12-10 14:05:10

问题


I have a form for logging into my website. I need to make it so that when the user hits enter, the form submits. How can I do this? Please provide code. Thanks.

          <form id="login" action="myHome.php" method="POST">
            <input type="text" name="email" id="email"/>
            <br/>
            <br/>
            <input type="text" name="password" id="password"/>
          </form>

回答1:


Have you actually tried anything?

You need to add an <input type="submit"> and hide it with CSS so that the browser knows what to trigger when enter is pressed, yet still not show a button. For the sake of accessibility and ease of use, I'd show the button even if not that many people use it (enter is much nicer).




回答2:


add a handler to on keydown and check for keycode == 13. Make this submit the form like below

function addInputSubmitEvent(form, input) {
    input.onkeydown = function(e) {
        e = e || window.event;
        if (e.keyCode == 13) {
            form.submit();
            return false;
        }
    };
}



回答3:


This should happen by default. In my experience some browsers require an <input type=submit> field, but generally this is not a requirement.



来源:https://stackoverflow.com/questions/7210678/how-to-make-enter-the-submit-button-in-a-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!