jQuery函数在回调时返回undefined [英] jQuery function returns undefined on callback
本文介绍了jQuery函数在回调时返回undefined的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我整理了一个使用干净框架的小javascript文件,格式如下:
I've put together a little javascript file which uses a clean framework, here's how it's formatted:
var functionLibrary = {
validateFields:function(inputArray, result) {
$.each(inputArray, function(index, listItem) {
if (!$.trim($("#" + listItem).val()).length) {
return "empty";
}
});
}
}
这是我将数据传递给此类函数并在验证响应时获得undefined
的原因:
Here's me passing data to such function and getting an undefined
when validating the response:
var Validation = functionLibrary.validateFields(['amount', 'ppemail']);
console.log(Validation);
为什么该函数应在返回empty
时在控制台日志中显示undefined
?
Why does such function show undefined
in the console log when it should be returning empty
?
推荐答案
您没有从validateFields
方法返回任何内容...
You are not returning anything from the validateFields
method...
var functionLibrary = {
validateFields: function (inputArray, result) {
var value;
$.each(inputArray, function (index, listItem) {
if (!$.trim($("#" + listItem).val()).length) {
//set the validity status
value = "empty";
//stop further execution of the loop
return false;
}
});
//return the value from validateFields
return value;
}
}
这篇关于jQuery函数在回调时返回undefined的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文