我每个循环都有一个条件:if(flag)
问题在于,jquery在循环结束之前运行条件(在控制台中,我可以在“ console.log(flag);
”之前看到“ console.log(data);
”)
仅在循环结束时如何运行它?
var flag = true;
$('.fileLink').each(function()
{
url = this.href;
var fileName = $(this).attr("data-fileName");
formData = "email= " + email + "&url=" + url + "&fileName=" + fileName;
$.ajax({
type : 'POST',
url : 'ajax/emailFiles.php',
data : formData,
dataType : 'json',
code : true
})
.done(function(data) {
console.log(data);
if ( !data.success)
flag = false;
});
});
console.log(flag);
if (flag)
$("#emailDocks_form-msg").html('<div class="alert alert-success">SUCCESS</div>');
else
$("#emailDocks_form-msg").html('<div class="alert alert-danger">ERROR</div>');