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>
4

1 回答 1

1

是的,这是因为您的嵌入式代码没有在输出中转义。使用 JSON:

optionsVal= '<input type="button" onclick=\'postRun('
    + JSON.stringify(value1) 
    + ')\' value="test" />';
于 2012-04-23T03:31:30.417 回答