Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do some forms auto-submit on clicking the enter button and others do not?

Problem:

I never specify a submit button, as I use ajax to submit my forms, but some forms have an auto-submit feature regardless.

Background:

Throughout my application I have instances when clicking the enter button will auto-submit a form and other instances where it will do nothing. For example; there are times where I will need to capture the "enter" button to get an auto-submit but other times where it seems to just happen on it's own and I have not found the pattern. Except that the dynamically created forms seem to have the auto-submit feature but static ones do not? Does anyone else have a similar issue.

Example:

All of my forms have the submit button removed and I specify no target or action elements as seen below. All of the forms have their data transmitted through AJAX.

<form id="ajaxForm">
<input>
</form>
<button>ButtonOutSideForm</button>

Not looking for a way to prevent auto-submit

I already know about "onsubmit = return false" and e.preventDefault. I'm not looking for a way to disable auto-submit. My question is why does it's presence seem arbitrary. Thanks.

like image 830
Usman Mutawakil Avatar asked May 01 '13 02:05

Usman Mutawakil


People also ask

How do I stop form submit on Enter key press?

$('form'). bind("keypress", function(e) { if (e. keyCode == 13) { e. preventDefault(); return false; } });

Should forms submit on enter?

When filling out a form, pressing enter in a textbox should submit the form. This is known as an implicit form submission.

How do you prevent form submit on Enter in JavaScript?

This can be solved by writing the <input type='button'/> with javascript, and then put an <input type='submit' /> within a <noscript> tag. The drawback of this approach is that for javascript-disabled browsers you will then have form submissions on ENTER.

How do I stop automatic form submission?

We use the preventDefault() method with this event to prevent the default action of the form, that is prevent the form from submitting. Example: HTML.


1 Answers

As LouD pointed out. Some browsers will auto-submit if the form only contains one input element.Why does forms with single input field submit upon pressing enter key in input

like image 149
Usman Mutawakil Avatar answered Sep 28 '22 00:09

Usman Mutawakil