开发者

How to make enter the submit button in a form

开发者 https://www.devze.com 2023-03-30 21:52 出处:网络
I have a form for logging int开发者_JAVA百科o 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.

I have a form for logging int开发者_JAVA百科o 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.

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


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


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号