Stop user from using enter to pass a form

2019-07-21 17:18发布

问题:

Due to an issue relating to a horrible mobile device (that work has given me no choice but to work with) I have to stop the use of the enter key from registering in form in HTML. The only way I'd like the data to be submitted is when the submit button is pressed.

Hope this is enough to help work out my problem! If not feel free to ask more.

回答1:

See here for an example.



回答2:

Try this submit button:

<input type="button" value="Submit" onclick="document.forms[0].submit();" />

Instead of using type="submit", type="button" won't bind the enter key to this button.



回答3:

You can use an onsubmit handler to prevent the form from being submitted in cases where it wasn't submitted in the way you wanted it to be. Just return false from the handler to cancel the submission.



标签: html forms