Access Javascript variables dynamically

后端 未结 4 1567
刺人心
刺人心 2020-11-28 16:07

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

Here\'s my simple problem:

var option1 = \"some text         


        
4条回答
  •  庸人自扰
    2020-11-28 16:44

    If they are variables in the window scope, then you can access window['option'+i]. However, you really should just use an array:

    var option = [
        "some text",
        "option 2",
        "option 3"
    ];
    for( var i=0; i<3; i++) alert(option[i]);
    

提交回复
热议问题