在两个javascript函数之间传递字符串

时间:2012-04-23 03:25:17

标签: javascript string jsp parameters

<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 个答案:

答案 0 :(得分:1)

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

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