2012-06-29 56 views
19

我有一個對象如何使用可變

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; 

回答

42

方括號:

message = Messages[ attribute ]; 
+2

................. :) –

5
var message = Messages[attribute]; 
+0

........... ........ :) –