简体   繁体   中英

Form submit with enter key acts wrong

I am using laravel and sammy.js for my application. My login form looks like this:

<form action="#/login" method="post">
<!-- inputs -->
</form>

Now, sammy.js catches it like this:

this.post('#/login',function(){
         //handle, send to laravel for login
        });

My problem is that if I press the enter key, apparently instead of submitting the form, which would result in this.post('#/login') event to be catched and the function to be executed, an HTTP request is already made, and the laravel route is requested. As the route does not exist, a MethodNotAllowedHttpException is thown.

Now, the question is: why does this happen? While pressing the "submit" button makes the login, hitting the enter key results in the error above.

I would like an actual solution to the problem, as well as an explanation of it, not patches like e.preventDefault() on keypress or return false in js.

Note: sammy is initialized correctly, the form is in the container on which sammy works and submitting using the enter key used to work in a previous version of the site. A lot has changed by now, so reverting is not a good option, so I would like an actual suggestion on how to solve the problem.

Thanks

citing section 4.10.22.2 Implicit submission in the Html5 specification:

User agents may establish a button in each form as being the form's default button. This should be the first submit button in tree order whose form owner is that form element, but user agents may pick another button if another would be more appropriate for the platform. If the platform supports letting the user submit a form implicitly (for example, on some platforms hitting the "enter" key while a text field is focused implicitly submits the form), then doing so must cause the form's default button's activation behavior, if any, to be run.

In a nutshell, hitting the Enter key will always submit the form (issue an Http request) regardless of SammyJs. Note that the enter key will submit the form even if there's not Submit button!

Are you returning false from the #/login Sammy route?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM