Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery override form submit not working when submit called by javascript on a element

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?

like image 578
user16624 Avatar asked Sep 26 '08 21:09

user16624


People also ask

Why form is not submitting in jQuery?

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.

Is jQuery submit deprecated?

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 .

What does JavaScript submit () do?

submit() allows to initiate form sending from JavaScript. We can use it to dynamically create and send our own forms to server.

How do I stop form submit in jQuery?

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


1 Answers

A couple of suggestions:

  • Overwrite the submit function to do your evil bidding
     var oldSubmit = form.submit;     form.submit = function() {         $(form).trigger("submit");         oldSubmit.call(form, arguments);     } 
  • Why not bind to all the <a> tags? Then you don't have to do any monkey patching, and it could be as simple as (assuming all the links are inside the form tag):
     $("form a").click(function() {         $(this).parents().filter("form").trigger("submit");     }); 
like image 55
stechz Avatar answered Oct 18 '22 19:10

stechz