我可以这样做:
validator.showErrors({ "nameOfField" : "ErrorMessage" });
这工作正常,但如果我尝试做这样的事情:
var propertyName = "nameOfField"; var errorMessage = "ErrorMessage"; validator.showErrors({ propertyName : errorMessage });
它会抛出'element is undefined'错误.
关于什么:
var propertyName = "nameOfField"; var errorMessage = "ErrorMessage"; var obj = new Object(); obj[propertyName] = errorMessage; validator.showErrors(obj);
值得注意的是,以下三种语法是等效的:
var a = {'a':0, 'b':1, 'c':2}; var b = new Object(); b['a'] = 0; b['b'] = 1; b['c'] = 2; var c = new Object(); c.a = 0; c.b = 1; c.c = 2;