I've been reading a lot before posting this question but I have not find the solution yet.
I've got a form with 3 fields (input type="text") with 'blur' event triggering a separate ajax. (Ajax will check for existence of the inputed data). On form submit, i would like to trigger the 'blur' events of these three fields (hence triggering the 3 ajax) and then if no error is found, the submit will proceed. I have used the jQuery.active variable to know if the 3 ajax requests are complete before checking for errors. But it does not work on chrome.
The Form:
<form id="theForm">
<input type="text" name="loginname" class="checkIfLoginnameExist" />
<input type="text" name="name" class="checkIfNameExist" />
<input type="test" name="phone" class="checkIfPhoneExist" />
</form>
The Ajax:
jQuery('.checkIfLoginnameExist').blur(function() {
var lname = jQuery(this).val().trim();
jQuery.ajax({
url: '<the URL>',
type: 'POST',
cache: false,
data: 'loginname=' + lname,
success: function() {
//--logic, if error found, add a span.validation-error beside the input
},
fail: function() {
--logic
}
});
});
On Submit logic:
jQuery('#theForm').submit(function(e) {
e.preventDefault();
jQuery(this).find('input').each(function() {
jQuery(this).blur(); // ------------ this will trigger the ajax
});
alert('before ' + jQuery.active);
while(jQuery.active > 0) {
//do nothing
alert(jQuery.active);
}
alert('after ' + jQuery.active);
//-- check for span.validation-error
//-- return true of false depending on span.validation-error
});
For IE and FF, the code works as expected, but in Chrome, it's stuck in the while(jQuery.active > 0) because the jQuery.active is not decremented, it's fixed on '3'.