我现在无法准确回答你的问题,因为你问的是如何使用无效的语法,这是不可能的,因为它是无效的。
但是,我可以做的是解释 JSON 在 JavaScript 中是如何工作的,并希望它能解决你的问题:
基础知识
{} 是一个对象字面量
//this creates a new object
a = {};
//so does this
a = new Object();
[] 是一个数组字面量
//this creates a new array
a = [];
//so does this
a = new Array();
属性可以通过.和[]符号访问:
//these are the same
a.b = c;
a['b'] = c;
可以使用文字值或字符串来设置对象文字:
a = {
//any character you can use for a variable name can be
//used to instantiate an object without quotes
b: c,
//special characters need to be quoted
"foo bar baz": "fizz buzz"
};
您的原始语法:
rules: {
jform[name]: {
required:true,
minlength:5,
maxlength:15
}
无效,因为您不能在变量名中使用 [ 和 ] 字符,但是您可以使用字符串作为文字值:
rules: {
"jform[name]": {
required:true,
minlength:5,
maxlength:15
}
...将被访问为:
rules["jform[name]"]
但您似乎希望以如下方式访问数据:
rules.jform[name]
需要设置为:
rules: {
jform: {}
}
...more code...
rules.jform[name] = {rules: {
required:true,
minlength:5,
maxlength:15
};