I've got a page with a normal form with a submit button and some jQuery which binds to the form submit event and overrides it with e.preventDefault()
and runs an AJAX command. This works fine when the submit button is clicked but when a link with onclick='document.formName.submit();'
is clicked, the event is not caught by the AJAX form submit event handler. Any ideas why not or how to get this working without binding to all the a elements?
The NUMBER ONE error is having ANYTHING with the reserved word submit as ID or NAME in your form. If you plan to call . submit() on the form AND the form has submit as id or name on any form element, then you need to rename that form element, since the form's submit method/handler is shadowed by the name/id attribute.
No, it's not deprecated! Deprecated = Not current. Obsolete = no longer available. IMHO the reason is because as browsers/html standards add more events, the team doesn't want to keep adding aliases when .
submit() allows to initiate form sending from JavaScript. We can use it to dynamically create and send our own forms to server.
We use the preventDefault() method with this event to prevent the default action of the form, that is prevent the form from submitting.
A couple of suggestions:
var oldSubmit = form.submit; form.submit = function() { $(form).trigger("submit"); oldSubmit.call(form, arguments); }
$("form a").click(function() { $(this).parents().filter("form").trigger("submit"); });
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