有人知道如何使用字符串来访问 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/
最佳答案
使用这个语法:
varanswer_select=answers[abcd];
关于javascript - 如何使用字符串访问 javascript 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25020059/