我正在编码为表单制作简单的验证器。
为了动态验证表单,我计划使用 eval 函数。
正如您在 javascript 编码中看到的,var funcCall 成为带有 jquery selected dom 参数的函数名称。
但这会出错。(未捕获的 SyntaxError:意外的标识符)
我想知道的是我应该只在 eval() 中使用 String 还是有更好的方法来做到这一点?
var Submit = {
validateFilters : ["Submit.emptyCheckFilter"], // This is validator filters
emptyCheckFilter : function(element)
//this is filter to validate if it's empty or not.
console.log("this has been called");
},
doValidate : function(form) {
var children = $(form).children();
var filters = Submit.validateFilters;
$.each(children, function(key, value) { // Loop over form element
for(var i = 0; i < filters.length; i++) {
//Here's error comes the value is not String. it's selected item by jQuery.
//Uncaught SyntaxError: Unexpected identifier <= This occurs.
var funcCall = filters[i] + "(" + value + ");";
// Call the filter
eval(funcCall);
}
return false;
});
},
...
}