2012-04-23 53 views
0
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Insert title here</title> 
<script type="text/javascript"> 
var ts=''; 
function test(value1) 
{ 
// this works when the value1 is a number only but if value1 is a string is does not work 
    var optionsVal = ' '; 
    optionsVal= '<input type="button" onclick="postRun('+value1+')" value="test" /> '; 
    document.getElementById('test').innerHTML = optionsVal; 
} 
function postRun(km) 
{ 
alert(km); 
} 

</script> 
</head> 
<body> 
<% 
String ss="Click Me"; 
%> 
<input type="button" onclick="test('<%=ss%>')" value="Click me" /> 

<div id="test"></div> 

</body> 
</html> 
+1

和你的問題是? – Joseph 2012-04-23 03:27:43

回答

1

是的,這是因爲您的嵌入代碼未在輸出中轉義。使用JSON:

optionsVal= '<input type="button" onclick=\'postRun(' 
    + JSON.stringify(value1) 
    + ')\' value="test" />'; 
+0

它的工作原理!謝謝@Thomas Allen – merz321 2012-04-23 03:37:04

+0

確保使用json2庫爲舊版瀏覽器提供JSON。 – 2012-04-23 04:06:46

相關問題