I need to check if the form is submitted by the user and then perform some operation before subsequently submitting the form programmatically.
So I have this code:
$('form').submit(function (e) {
e.originalEvent && e.preventDefault();
//I change some things in the form here.
!e.isTrigger && $(this).submit();
});
This works fine, but when the form submit button is named "submit" it does not.
It even works ok if the button is named "Submit". (uppercase "S")
I'm just curious as to why this is?
Here is a demo fiddle
Sometimes the problem is caused by old versions of the Javascript files, cached by your browser and can be fixed by clearing the browser cache. You can use the browser console of your browser for debugging. After the Javascript error is fixed, the submit button will automatically be enabled.
The <input type="submit"> defines a submit button which submits all form values to a form-handler. The form-handler is typically a server page with a script for processing the input data. The form-handler is specified in the form's action attribute.
The submit event fires when the user clicks a submit button ( <button> or <input type="submit">) or presses Enter while editing a field (e.g. <input type="text">) in a form. The event is not sent to the form when calling the form. submit() method directly.
The Submit Button The <input type="submit"> defines a button for submitting the form data to a form-handler. The form-handler is typically a file on the server with a script for processing input data. The form-handler is specified in the form's action attribute.
You fell prey to some ancient DOM quirks. Namely, forms in the DOM get all their input elements (and buttons, too) exposed as properties by their respective name:
<form>
<input name="foo">
</form>
results in:
var form = document.forms[0];
form.foo === document.getElementsByName('foo')[0];
which is unfortunate, when you have any control with the name submit
, because it shadows your original form's submit method.
Edit: By the way, for reading on many of those pitfalls I suggest to take a look at Kangax' fantastic DOMLint documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With