我使用的onsubmit =「返回check_for_conflict()」來調用這個函數:停止表單提交根據Ajax響應
function check_for_conflict(){
var client_id = $('#agent_select').val();
//Stop submit if no agent selected.
if (client_id == 0){
alert("Please select an agent.")
return false;
}
var duration = $('#select_job_duration').val();
var id = $('#input_id').val();
var dat = $('#input_date').val();
var st_time = $('#input_st_time').val();
//Check if scheduling conflict and stop submit if there is a conflict.
$.post("check_for_conflicts.php", { job_duration: duration, provider_id: id, job_date: dat, job_st_time: st_time },
function(data){
console.log(data.status);
if(data.status == 'conflict'){
alert("There is a conflict with this time.")
return false;
}
}, "json"
);
//Continue submit if now scheduled conflicts.
return true;
}
我有兩個檢查,以阻止表單提交。第二次檢查取決於.post響應。這部分不起作用,因爲它在.post完成之前達到了最終的「返回true」。如何使用.post響應來停止提交表單?