Javascript:由另一个变量的值组成的新变量名称
通过显示代码最容易解释:
var delta_selected = false;
var magma_selected = false;
var aqua_selected = false;
// Somewhere in my functions...
if (sumthing == 'sumthing1') {
topic = 'delta';
} else if (sumthing == 'sumthing2') {
topic = 'magma';
} else {
topic = 'aqua';
}
// Then assign it (so if topic is delta, then delta_selected = true)
topic + "_selected" = true;
最后一行不起作用。语法错误。
我需要帮助来弄清楚如何做到这一点。谢谢!
Easiest to explain by showing code:
var delta_selected = false;
var magma_selected = false;
var aqua_selected = false;
// Somewhere in my functions...
if (sumthing == 'sumthing1') {
topic = 'delta';
} else if (sumthing == 'sumthing2') {
topic = 'magma';
} else {
topic = 'aqua';
}
// Then assign it (so if topic is delta, then delta_selected = true)
topic + "_selected" = true;
This last line isn't working. Syntax error.
I need help to figure out how to do this. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个
try this
为什么使用变量?在我看来,您需要一个包含所有
_selected
值的结构。然后...
Why use variables? Looks to me like you need a single structure that contains all your
_selected
values.then...
看一下“eval()”函数。您可能最终会得到类似的结果:
但请检查文档以确保确定。
希望这有帮助。
Take a look at the "eval()" function. You will probably end up with something like:
But check the documentation to be sure.
Hope this helps.