动态访问Javascript变量 [英] Access Javascript variables dynamically

查看:122
本文介绍了动态访问Javascript变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道你可能认为这是一个重复或愚蠢的问题。但答案对我没有帮助。

I know you might think this is a duplicate or a dumb question. But the answers doesn't help me.

这是我的简单问题:

var option1 = "some text";
var option2 = "some text";
var option3 = "some text";

我想在for循环中访问元素option1,option2,option3:

I want to access the elements "option1, option2, option3" in a for loop:

for(var i = 1; i < 4; i++)
{
    alert(option+i);   
}

我知道它不应该是选项+我但我不知道如何要解决这个问题。

I know that it shouldn't be option+i but I dont know how to solve this.

感谢您提前获得帮助。

如果您发现这只是重复的话标记这个问题。谢谢

If you find this to be a duplicate just mark this question. Thanks

推荐答案

请试试这个:

    option = new Array();
    option[1] = "some text";
    option[2] = "some text";
    option[3] = "some text";
    for(var i = 1; i < 4; i++)
    {
        alert(option[i]);   
    }

这篇关于动态访问Javascript变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆