开发者

Safari: Disable form-submit on Enter? [duplicate]

开发者 https://www.devze.com 2022-12-14 05:15 出处:网络
This question already has answers here: Prevent users from submitting a form by hitting Enter (36 answers)
This question already has answers here: Prevent users from submitting a form by hitting Enter (36 answers) Closed 7 years ago.

Following problem:

On my site, I have two forms. One f开发者_运维问答or login and one for main data. I have only one submit button in the mainform. The loginform is submitted via js on mainform submit.

This works fine. But in Safari I can press Enter while my focus is inside the login form. This will send the login form without the mainform, so I get an error (It redirects me to a plain view of a json I request on submit).

Is it possible to disable the "on enter pressed" action?


If you say you use anyway the JS to submit the data in the form why not cancel the default behaviour of the browser with <form onsubmit="return false;">. This way no matter if you press the submit button or press enter in an input it won't be sent. The onsubmit="return false" shouldn't interfere with calling the submit() method on the form from JS.


You can add the onsubmit handler for your login form. This handler can help you prevent submitting your form (and for example move your cursor to the next empty field in the forms);

<form onsubmit="do_some_function(); return false;" ></form>

To prevent form from submition, return false in your onsubmit handler;

0

精彩评论

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