我有一些jQuery,它使用每個循環來檢查在Symfony 3 CRM上的重複表單字段中輸入的值。有一個$.post
它將輸入的值發送到一個函數,該函數檢查數據庫中的重複項,如果它是重複項,則會向數組中添加一些內容,否則會添加一個空值以指示它不是重複項。完成這些操作後,它會檢查最終數組並將任何錯誤添加到錯誤塊以顯示給用戶。每個jQuery調用都會在完成之前繼續運行
但是,它似乎總是空白,我相信它是因爲它運行的代碼顯示錯誤之前,它實際上完成獲取響應。
這裏是我的代碼:
$('#puppy_form').on('submit', function() {
var bitch_errors = [];
var dog_errors = [];
// NOTE: Bitch and dog names need to be checked differently so we know which error is assigned to which input
$('.check_bitch_name').each(function(i, obj) {
// need to check each name for validity and duplication.
var entered_bitch_name = obj.value;
var pattern = /^[a-zA-Z,.]+\s[a-zA-Z,.]+(\s[a-zA-Z,.]+){0,}$/;
if(!pattern.test(entered_bitch_name)) {
bitch_errors[i+1] = "invalid";
} else {
// now to check for duplicates
$.post('/check-puppy-name', { name: entered_bitch_name }
).done(function (response) {
if(response == 'duplicate') {
bitch_errors[i+1] = "duplicate";
} else {
bitch_errors[i+1] = "";
}
});
}
});
$('.check_dog_name').each(function(i, obj) {
// need to check each name for validity and duplication.
var entered_dog_name = obj.value;
var pattern = /^[a-zA-Z,.]+\s[a-zA-Z,.]+(\s[a-zA-Z,.]+){0,}$/;
if(!pattern.test(entered_dog_name)) {
dog_errors[i+1] = "invalid";
} else {
// now to check for duplicates
$.post('/check-puppy-name', { name: entered_dog_name }
).done(function (response) {
if(response == 'duplicate') {
dog_errors[i+1] = "duplicate";
} else {
dog_errors[i+1] = "";
}
});
}
});
if(count(bitch_errors) == 0 && count(dog_errors) == 0) {
return true;
}
// loop through the errors and assign them to the correct input
$.each(bitch_errors, function(key, value) {
if (value == "invalid") {
$('input[name="bitch_name['+key+']"]').parent().addClass('has-error');
$('input[name="bitch_name['+key+']"]').next('.error-message').html('Names must be at least two words, and only contain letters');
return false;
} else if(value == "duplicate") {
$('input[name="bitch_name['+key+']"]').parent().addClass('has-error');
$('input[name="bitch_name['+key+']"]').next('.error-message').html('Sorry, this name has already been taken');
return false;
}
});
$.each(dog_errors, function(key, value) {
if(value != "") {
if (value == "invalid") {
$('input[name="dog_name['+key+']"]').parent().addClass('has-error');
$('input[name="dog_name['+key+']"]').next('.error-message').html('Names must be at least two words, and only contain letters');
return false;
} else if(value == "duplicate") {
$('input[name="dog_name['+key+']"]').parent().addClass('has-error');
$('input[name="dog_name['+key+']"]').next('.error-message').html('Sorry, this name has already been taken');
return false;
}
}
});
return false;
});
基本上,它首先檢查輸入的名稱是否有效,然後投遞過和愚弄檢查。問題是,即使它進行了有效性檢查(並相應地打印了錯誤),它似乎忽略了欺騙檢查並繼續進行,甚至還沒有回覆第一個響應。
我該如何確定它在完成之前檢查並將錯誤添加到表單中?我試過其他解決方案,包括嘗試在jQuery中實現$.when
功能,但我並不真正瞭解如何使其工作。任何幫助讚賞。
我真的不明白這一點 - checkDogs的含義是什麼?如果我寫這個,這意味着之後沒有別的東西會運行,因爲它有回報嗎? –
@MichaelEmerson唯一需要在你的'submit'回調中的位是兩個調用'checkInputs'和最後一個'$ .when'的塊。正如你已經猜測的那樣,直到'$ .post'調用全部完成之後,這些數組纔可用,'$ .when'調用處理該同步。 – Alnitak
好吧,那很好,但checkDogs(數組)是什麼?如果我補充一點,它會在第一個括號之前拋出一個錯誤,指出「期待換行符或分號」。另外,由於返回,getInputValues永遠不會被達到? –