2012-06-29 15 views
19

Ho un oggettocome accedere proprietà dell'oggetto utilizzando variabile

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' 
}; 

e una variabile ..

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

ora voglio per selezionare i messaggi oggetto struttura utilizzando questa variabile come questo ..

var message = Messages.attribute; 

ma non funziona .. e hanno anche provato il seguente ..

var message = Messages+'.'+attribute; 

risposta

42

parentesi quadre:

message = Messages[ attribute ]; 
+2

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

5
var message = Messages[attribute]; 
+0

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

Problemi correlati