我试图从ajax成功函数返回值。但它什么也没返回。
JS
function calculate_total_percentage(course_log_id){
var total_percentage = 0;
$.ajax({
url:"teacher_internal_exam_management/get_exams_of_course_log/"+course_log_id,
type: "POST",
dataType: "json",
success: function (exams_of_course_log) {
for (var x = 0; x < exams_of_course_log.length; x++) {
total_percentage += parseInt(exams_of_course_log[x].marks_percentage);
}
alert(total_percentage);
return total_percentage;
}
});
}
Run Code Online (Sandbox Code Playgroud)
如果我那样打
alert(calculate_total_percentage(course_log_id));
Run Code Online (Sandbox Code Playgroud)
然后显示“ 61”(由于调用alert(total_percentage);),然后显示“ undefined”为什么?它应该两次显示“ 61”?问题是什么?