21

我有一个对象

var Messages = {
'fullName' : 'Tell us your cool name dude..! e.g. Yousuf Iqbal',
'userName' : 'Choose a catchy username. Remember! It should be available :)',
'password' : 'Choose a top secret password with special chars, numbers and alphabets',
'rePassword' : 'Retype the password you just typed. But, don\'t try to copy!',
'bYear' : 'Tell the year, in which this bomb blasted'
};

和一个变量..

var attribute = $('#userinfo form input').attr('name');

现在我想像这样使用这个变量来选择消息对象属性..

var message = Messages.attribute;

但它不起作用..并且还尝试了以下..

var message = Messages+'.'+attribute;
4

2 回答 2

45

方括号:

message = Messages[ attribute ];
于 2012-06-29T04:34:56.217 回答
6
var message = Messages[attribute];
于 2012-06-29T04:35:15.313 回答