Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery: on submit doesn't work

Tags:

jquery

What I am trying is to catch all the form submit events, get the url from the action attribute and use it to send the content of the form to that address using AJAX. So what I need is only one on submit event handler. However I quickly got in trouble as it seems to not be working in IE.

$(document).submit(function(e) {
        alert('clicked');
        e.preventDefault();
    });  

This is the code I use for cross-browser testing purpose. It works perfectly in Chrome, Firefox but not in IE. Am I not allowed to set an event listener on document?

like image 466
Andrew Avatar asked Jun 23 '12 20:06

Andrew


People also ask

Why form submit is not working 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.

Why is my submit button not working Javascript?

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.

Why Onsubmit is not being called?

The onsubmit handler is not called, because the form cannot be submitted by any normal means, i.e. the submit event cannot be caused. There is only one submit control, and it is declared as disabled. Save this answer.


2 Answers

Have a look at the jQuery API:

The JavaScript submit event does not bubble in Internet Explorer. However, scripts that rely on event delegation with the submit event will work consistently across browsers as of jQuery 1.4, which has normalized the event's behavior. (api.jquery.com/submit)

The submit event does not bubble in Internet Explorer, so the document element will never be notified of submit events. jQuery will normalise this for you, but only if you use event delegation. This is not difficult to do, and in fact may make your code nicer, if there is more complexity to it:

$(document).on('submit', 'form', function(e) {
    alert('clicked');
    e.preventDefault();
});

This keeps the advantages of binding to the document (e.g. capturing events on elements that don't yet exist), while making it possible in IE. Note that this, within the event handler, is now the form element, not the document.

like image 70
lonesomeday Avatar answered Oct 05 '22 22:10

lonesomeday


Attach the listener to your forms

$("form").submit(function(e) {
        alert('clicked');
        e.preventDefault();
}); 
like image 37
Zoltan Toth Avatar answered Oct 05 '22 21:10

Zoltan Toth