2012-06-29 33 views
19

tôi có một đối tượnglàm thế nào để truy cập vào tài sản đối tượng sử dụng biến

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

và một biến ..

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

bây giờ tôi muốn chọn Tin nhắn của đối tượng sở hữu sử dụng biến này như thế này ..

var message = Messages.attribute; 

nhưng không hoạt động .. và cũng đã thử cách sau ..

var message = Messages+'.'+attribute; 

Trả lời

42

dấu ngoặc vuông:

message = Messages[ attribute ]; 
+2

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

5
var message = Messages[attribute]; 
+0

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

Các vấn đề liên quan