2010-01-23 86 views
2

我將String參數傳遞給javascript。但它沒有被調用。這是我的腳本:將String參數傳入Javascript

function downloadPopup(testing){  
alert(testing); } 

我打電話從我的jsp頁面類似這樣的JavaScript:

<% String testing = "DSfsdsfd" ; %> <a 
href="javascript:downloadPopup(<%=testing%>)" 
> Click </a> 

我該如何解決呢?

回答

3

我認爲你缺少你周圍的字符串引號:

<% String testing = "DSfsdsfd" ; %> <a 
href="javascript:downloadPopup('<%=testing%>')" 
> Click </a> 
+0

感謝佩卡。這工作正常。 – 2010-01-26 18:50:46

0
downloadPopup('<%=testing%>') 

不要忘記把字符串 ''

+0

這工作,除非字符串本身有引號。無論您使用哪種語言或Web框架,都可能有一個「to_json」方法。在字符串上調用它並引用它,併爲您跳過引號。 – jpsimons 2010-01-23 22:55:03

+0

你是對的黑暗運動員。如果字符串中有引號,你會得到一個js錯誤。如果C#可以按照以下方式轉義引號。 '<%= testing.Replace(「'」,「\\\'」)%>' – 2010-01-24 11:03:27