2013-11-22 66 views
0

比方說,我有變量是這樣的:JQuery的結合變量名

localStorage.var1a = 0; 
localStorage.varnum = 1000; 

和代碼是這樣的:

for(x = 1; x <localStorage.varnum;x++){ 
    if(localStorage.var.x.a > 0){ 
    localStorage.var.x.a = Number(localStorage.var.x.a) 
    }else{ 
    localStorage.var.x.a = 0; 
    } 
} 

我的問題是,我不知道如何x和localStorage的結合。 var * a;

+0

*「我不知道如何結合字符串連接的x和localStorage.var * a」*。然後您可以使用括號表示法來訪問該屬性。請參閱https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Member_Operators。 –

回答

0
if(localStorage["var"+x+"a"] > 0){ 
    localStorage["var"+x+"a"] = Number(localStorage["var"+x+"a"]); 
    }else{ 
    localStorage["var"+x+"a"]= 0; 
    } 
+0

你爲什麼使用'eval'? –

+0

by「localStorage.var」的eval值+ x +「a」來吧 –

+0

爲什麼不使用'localStorage [「var」+ x +「a」]'? –