有没有人知道如何使用字符串来访问javascript对象? 我需要使用var / string来访问javascript对象。
++这个问题已经得到解答 - 请参阅JSFiddle以获取修复++
var answers = {"A":0,"B":0,"C":0,"D":0};
var abcd = $(this).attr("data-filter"); //this is equal to A
var answer_select = "answers."+abcd; //this is equal to answers.A
answer_select=20; //does not change the value.
//if i write..
answers.A=20; //it works
//I need to use the variable "answer_select" as this changes each time..
我在这里设置了一个jsfiddle ..
http://jsfiddle.net/jstleger0/h3SEX/6/
答案 0 :(得分:2)
使用以下语法:
var answer_select = answers[abcd];