如何通过jsp表达式标签将java字符串变量传递给javascript函数? [英] How to pass java string variable to the javascript function by jsp expression tag?
问题描述
我想使用jsp表达式标签将java字符串变量传递给javascript函数参数.下面是我的jsp页面.
I want to passing a java String variable to the javascript function parameter using jsp expression tag.Below is my jsp page.
First.jsp
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script>
function stringGenerate(str){
var x = document.getElementsByTagName("input");
x[0].value = str;
}
function numberGenerate(num){
var x = document.getElementsByTagName("input");
x[1].value = num;
}
</script>
</head>
<body>
<%
String num ="1234567890";
String str = "abcdefghij";
%>
<input type="text" name="string" readonly="readonly"/>
<input type="text" name="number" readonly="readonly"/><br/><br/>
<input type="button" value="String Generate" onclick= "stringGenerate(<%=str %>)" />
<input type="button" value="Number Generate" onclick= "numberGenerate(<%=num %>)" />
</body>
</html>
当我单击具有数字生成"值的按钮时,则num变量值(1234567890)将显示在文本框上(名称=数字"),但是当我单击具有值字符串生成"的按钮时,那么相应的文本框(名称="string")上没有任何显示.这里num和str都是字符串类型的变量,但是为什么在文本框中仅显示num变量值,为什么不显示str变量值?
When I click on the button with value "Number Generate",then the num variable value(1234567890) will display on the textbox(name="number") but when I click on the button with value "String Generate",then there is nothing display on the corresponding text box(name="string").Here both num and str are string type varible but why only num variable value is displayed on textbox and why not str variable value is displayed?
推荐答案
在HTML中使用字符串时,尝试使用单引号'
:
Try using single quote '
when using string in HTML:
onclick= "stringGenerate('<%=str %>')"
^ ^
完整代码:
<input type="button" value="String Generate" onclick= "stringGenerate('<%=str %>')" />
这篇关于如何通过jsp表达式标签将java字符串变量传递给javascript函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!