javascript 无法转换为对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4670093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
cannot convert to object
提问by dazz
if i execute the following code i get a cannot convert to object error; Uncaught exception: TypeError: Cannot convert 'validation.messages.field' to object
如果我执行以下代码,我会得到一个无法转换为对象的错误;未捕获的异常:TypeError:无法将“validation.messages.field”转换为对象
$.fn.validate = function(validation) {
$.each(validation.rules, function(field, fieldRules){
$.each(fieldRules, function(rule, ruleValue){
var fieldValue = $('[name=' + field + ']').val();
if (eval(rule + '(fieldValue, ruleValue)') == false){
alert(validation.rules.field.rule);
return false;
}else{
return true;
};
});
});
}
the problem is the
问题是
alert(validation.messages.field.rule);
alert(validation.messages.field.rule);
'field' = 'persoon_voornaam' and 'rule' = 'required'
and validation.messages.persoon_voornaam.requiredworks just fine.
'field' = 'persoon_voornaam' 和 'rule' = 'required' 并且validation.messages.persoon_voornaam.required工作得很好。
What am i doing wrong?
我究竟做错了什么?
validation is a JSON that look like this:
验证是一个如下所示的 JSON:
{
rules: {
persoon_voornaam: {
required: true,
minlength: 5,
},
postcode_bestemming: {
required: true,
minlength: 7,
},
},
messages: {
persoon_voornaam: {
required: 'Dit veld is verplicht',
minlengt: 'Dit veld moet minstens 5 lang zijn',
},
}
}
采纳答案by Martin Jespersen
It has to be:
它一定要是:
alert(validation.rules[field][rule])
To clarify:
澄清:
validation.rulesis an object while fieldis a variable with the string 'persoon_voornaam'.
validation.rules是一个对象field而是一个带有字符串的变量'persoon_voornaam'。
validation.rules.persoon_voornaamis an object and the same as validation.rules['persoon_voornaam'].
validation.rules.persoon_voornaam是一个对象并且与 相同validation.rules['persoon_voornaam']。
回答by Felix Kling
The answer to your problem was already given. I want to propose an improved version of your code, to make your code more reusable (otherwise there is not need to attach it to the jQuery object).
你的问题已经给出了答案。我想提出您的代码的改进版本,以使您的代码更具可重用性(否则无需将其附加到 jQuery 对象)。
- Your are not making use of the fact that this function will be applied to elements selected by your selector. You are selecting allform elements that match a certain name.
evalis evil. You would have to pollute the global namespace with functions namedrequiredandminlength.
- 您没有利用此功能将应用于选择器选择的元素这一事实。您正在选择与某个名称匹配的所有表单元素。
eval是邪恶的。您将不得不使用名为requiredand 的函数污染全局命名空间minlength。
The following code tries to adress these issues:
以下代码试图解决这些问题:
$.fn.validate = (function() {
var validators = {
'required': function(value, ruleValue) {
/*...*/
},
'minlength': function(value, ruleValue) {
/*...*/
}
};
return function(validation, config) {
return this.each(function() {
var errors = [];
var $element = $(this);
$.each(validation.rules, function(field, fieldRules){
$.each(fieldRules, function(rule, ruleValue){
var fieldValue = $element.find('[name=' + field + ']').val();
if (rule in validators && validators[rule].call(null, fieldValue, ruleValue) === false){
var message = (field in validation.messages && rule in validation.messages[field]) ? validation.messages[field][rule] : '';
errors.push([field, rule, message])
return false;
}
});
});
// the following part could be written shorter as
// var cb = erros.length > 0 ? 'onerror' : 'onsuccess';
// if(typeof config[cb] === 'function') {
// config[cb](errors);
// }
// but it will pass an empty array to success (which is not a problem
// I just though it might be confusing)
if(erros.length > 0) {
if(typeof config.onerror === 'function') {
config.onerror(errors);
}
}
else {
if(typeof config.onsuccess === 'function') {
config.onsuccess();
}
}
});
}
}());
Then you can call the function with:
然后您可以使用以下命令调用该函数:
$('#yourFormId').validate(valditation, {
onerror: function(errors) {
// do something with errors
},
onsuccess: function() {
// do something else
}
});
and it will only validate the fields in the form.
它只会验证表单中的字段。

