1

I have read following SO topic

and I have wrote following code:

$form = $("#form"); 
$form.bind('ajax:complete', function() {
            alert("done");
 });
 $form.submit();

But I don't see alert after submit click.

jsfiddle

Fiddle a bit wrong because I don't know endpoint where I can send request. but on my local mashine I see same result

Community
  • 1
  • 1
gstackoverflow
  • 31,683
  • 83
  • 267
  • 574

2 Answers2

3

Attach handler in you form's onsubmit event first, then inside that handler call ajax, when ajax is successful the display message done:

   $form = $("#form"); 
    $form.on("submit",function()
    {
       alert("submitting..");
       //do ajax
       $.ajax({
                 url:<submit url>,
                 type:post/get,
                 success:function() { alert("done");  }
             });
    });
     $form.submit();

Please read more about DOM event handling and ajax using JQuery/Javascript.

VMcreator
  • 1,705
  • 8
  • 17
2

jQuery 1.8 changed this nature, from that version onwards the ajaxComplete and other global handlers can be attached to the document object only

$(document).ajaxComplete(function () {
    alert("done");
});

ajaxComplete

As of jQuery 1.8, the .ajaxComplete() method should only be attached to document.

Demo: Fiddle

Arun P Johny
  • 365,836
  • 60
  • 503
  • 504