我在表单提交时收到“太多递归”的错误,这里是我的代码,任何人都可以告诉我什么是问题?
$("#ajax-post-form").on('beforeSubmit', function (e) {
var postType = $("#post-posttype").val();
var postVideo = $("#post-postvideo").val();
var postImages = $("#post-postimages").val();
if(postVideo == '' && postType == 3) {
alert("Please upload video");
return false;
}
else if(postType == '1' || postType == '2') {
if(postType == '2' && postImages == '') {
alert("Please upload image");
return false;
} else {
$("#spinnerLoader").show();
$("#ajax-post-form").submit();
return true;
}
}
答案 0 :(得分:0)
缩小到最小的工作范例......
$("#ajax-post-form").on('beforeSubmit', function (e) {
$("#ajax-post-form").submit(); //triggers 'beforeSubmit' event and causes recursion
})
您可能希望在尝试重新提交之前取消挂起beforeSubmit
处理程序...
...或添加一个标志来表示重新提交:
var resubmit = false;
$("#ajax-post-form").on('beforeSubmit', function (e) {
if(resubmit)return;
resubmit = true;
$("#ajax-post-form").submit(); //this triggers 'beforeSubmit' event
resubmit = false;
})