jQuery ajax check and submit form, always have to submit twice when ajax return nofound
I have this ajax check to see if the name is found or nofound. It works fine when data retur开发者_StackOverflow中文版ns found, but when it return nofound, I must submit it again to trigger it.
Does anyone have a suggestion or a better solution for this?
$('#formletter').submit(function() {
var name = $('#name').val();
if ($.formLoading != false) {
$.ajax({
type: 'POST',
url: "submit_ajax_contents.php",
data: "namecheck="+name,
success: function(data) {
if(data == 'nofound'){
$.formLoading = false;
$('#formletter').submit();
}else{
alert('found');
$.formLoading = true;
}
}
});
return false;
} else {
return true;
}
});
});
Many thanks :)
You might want to do a non-async ajax call: (I didn't test this code)
$('#formletter').submit(function(event) {
var name = $('#name').val();
var found = false;
$.ajax({
async: false,
type: 'POST',
url: "submit_ajax_contents.php",
data: "namecheck="+name,
success: function(data) {
if(data == 'found')
found = true;
}
});
if (found)
event.preventDefault();
});
精彩评论