jquery
2012-11-29 107 views 0 likes 
0

我的問題是讓我瘋狂。Jquery變量輸出

我宣佈索姆變量這樣

var tip01 = "This is my first tip"; 
var tip02 = "This is my second tip"; 

根據我的查詢字符串我想寫出我的建議值。

我的查詢字符串:前myQSvalue = 01

var myQSvalue = getQueryString('myQSvalue'); 
var myTip = 'tip' + myQSvalue; 

$("#output").html(myTip); 

但輸出是:tip01 insted的的This is my first tip

我該如何實現這一點並輸出我聲明的可擴展內容?

謝謝。

回答

4

,而不是不同的變量都,你應該使用一個數組

var myTips = []; 
myTips[0] = "This is my first tip"; 
myTips[1] = "This is my second tip"; 

然後在查詢字符串傳遞一個指數?myQSvalue=0

而且從陣列讀取:

var myQSvalue = parseInt(getQueryString('myQSvalue'),10); 
$("#output").html(myTips[myQSvalue]); 
+0

謝謝你,我用數組方法去了。 –

0

我會建議在數組中定義提示,但應該能夠使用:

$("#output").html(window[myTip]); 
0

正確的做法已經指出的Jamiec,但只是以供將來參考,你可以參考全局變量作爲全球範圍內的性能:

var myQSvalue = getQueryString('myQSvalue'); 
var myTip = 'tip' + myQSvalue; 

$("#output").html(window[myTip]); 
0

感謝您的幫助。我採用了陣列方法,並且效果很好。但是$(「#output」)。html(window [myTip]);沒有爲我工作。

在某些情況下,這種方法會很方便。

相關問題