1

我正在努力实现以下目标:

this.inputs[options.el.find('form').attr('class')] = {};

this.inputs[options.el.find('form').attr('class')][options.elements[x].selector] = false;

但是,如果没有语法错误,我将无法执行上述操作!

有什么想法可以实现这个对象结构吗?

4

2 回答 2

2

这种语法看起来是合法的,但是这些长的单行代码并没有给任何人带来任何好处。把它拆开一点,这样你就可以找到它失败的地方。

var className = options.el.find('form').attr('class');
var selector = options.elements[x].selector;

this.inputs[className] = {};
this.inputs[className][selector] = false;
于 2013-01-21T10:19:29.870 回答
0

对象的索引应该始终是一个字符串:

var obj = {
  a: 2;
}

obj.a = 3; //a is 3 now
obj['a'] = 4;//a is 4 now

你说options.elements[x].selector的是input[name="username"]所以我猜在这一行:

this.inputs[options.el.find('form').attr('class')][options.elements[x].selector] = false;

你真正想做的是:

this.inputs[options.el.find('form').attr('class')]['username'] = false;

所以你可以这样做:

var sel = options.elements[x].selector;
console.log( 'sel is ' + sel );
this.inputs[options.el.find('form').attr('class')][sel] = false;

确保 sel 是一个字符串。你可能想试试

var sel = options.elements[x].selector.value;

.value位从输入元素内部检索文本。

于 2013-01-21T10:39:03.987 回答