我有一个父函数,它包含一个ajax调用以及一个在ajax调用之前的验证函数。我想要实现的是显然首先运行验证函数,如果其中任何一个失败,则阻止进一步执行父函数。
因此,如果我返回null
会停止进一步执行该函数,但是如何从子函数返回null
以影响父函数呢?
子功能(验证)
function trrValidationError(ele, title, text) {
if ($(ele).val() === "") {
if (title === null) {
title = "Warning";
}
if (typeof text == "undefined") {
text = "Input validation error!";
}
swal({
title: title,
text: text,
type: "error"
});
return null;
}
}
家长功能
EventAdd: function(ele) {
var form = $(ele).closest("form")[0];
var formData = new FormData(form);
trrValidationError($("#webcms-events-description"),null,"Description field must not be empty!"));
trrValidationError($("#StartDateTime"),null,"Start Date field must not be empty!"));
trrValidationError($("#EndDateTime"),null,"End Date field must not be empty!"));
if (trrConfirm("",
"Are you sure you want to add a new event?",
function() {
$.ajax({
type: "POST",
data: formData,
url: $(ele).data("url"),
enctype: "multipart/form-data",
processData: false,
contentType: false
})
.done(function(response) {
swal({
text: "You have successfully added a new event!",
type: "success"
});
form.reset();
var newele = $("<div/>");
newele.html(response).contents().insertAfter($("#webcms-events-table-row"));
})
.fail(function(xhr, status, error) {
trrAlert(xhr.responseText);
});
}));
},