[英]Javascript return true or false to callback from another function
我正在使用具有回調submitHandler的jQuery validate插件。 在該回調中,如果它返回false,則中止提交,如果返回true,則使用post方法提交。
$("#collection_form").validate({
submitHandler: function(form) {
pre_ajax_submit(function (pre_ajax_return) {
if (pre_ajax_return == 'non_ajax') {
//something
} else {
//other
}
});
}
});
所以我的問題是:我在回調pre_ajax_submit()
有一個函數。 如何在主回調中使函數返回true或false。 我希望得到與此相同的結果:
$("#collection_form").validate({
submitHandler: function(form) {
return true;
}
});
只需顛倒執行順序即可。 在 AJAX之前的任務完成后驗證表單:
pre_ajax_submit(function (pre_ajax_return) {
if (pre_ajax_return == 'non_ajax') {
//something
} else {
//other
}
$("#collection_form").validate({
submitHandler: function(form) {
return true;
}
});
});
我猜你的代碼中可能還有很多其他的事情發生。 你應該考慮使用Promises:
var whenPreAJAXFinished = new Promise(function(resolve, reject){
pre_ajax_submit(function (pre_ajax_return) {
if (pre_ajax_return == 'non_ajax') {
//something
} else {
//other
}
resolve()
})
})
whenPreAJAXFinished.then(function(){
$("#collection_form").validate({
submitHandler: function(form) {
return true;
}
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.