javascript - Stall submitting of a form with js/jquery? -
i have few ajax requests need complete before can submit form.
how can stall within form.submit() call until ajax requests done?
var incompleteajaxcalls = 0 // each ajax call runs incompleteajaxcalls++ // followed incompleteajaxcalls-- upon completion myform.submit(function () { //sleep while incompleteajaxcalls > 0; });
prevent form submitting, submit when ajax calls have completed
myform.submit(function (e) { e.preventdefault(); var self = this; $.when( $.ajax({url: 'ajax1'}), $.ajax({url: 'ajax2'}) ).always(function() { self.submit(); }); });
edit:
for array do
myform.submit(function (e) { e.preventdefault(); var self = this, xhr = []; xhr.push( $.ajax({url: 'ajax1'}) ); xhr.push( $.ajax({url: 'ajax2'}) ); $.when.apply($, xhr).always(function() { self.submit(); });
Comments
Post a Comment