在jsp中经常会遇到把js变量赋给java变量,或者将java变量赋给js变量的情况,在此将通用的处理方法小结如下:
1、java变量传给js写法如下:
var a="<%=javaParam%>"
注意这里要将引号""加上;
2、js变量给java稍微复杂点,一般是在表单中用一个
的隐藏表单元素,然后在脚本中将js变量值赋给它:
var jsParamValue='aaaa'
form1.jsParam.value=jsParamValue
然后就可以request.getPrameter("jsPrama")
来取得js变量值了。
Java 代码如下:<%
//得到要转换的数组---当然这里也可以是其他类型的数组
List jsList = (List) request.getAttribute("gczbh")
%>
JS 代码如下:
<script type="text/JavaScript">
function initPages()
{
var codes = new Array()
// 将JAVA中的数组转换成JS 的数组
<%
if(jsList!=null)
{
for(int i=0i<jsList.size()i++)
{
%>
codes[<%=i%>]='<%=jsList.get(i)%>'
<% }
}
%>
</script>
<!DOCTYPE HTML><html>
<head>
<title>Page Title</title>
<meta http-equiv="Content-Type" content="text/html charset=UTF-8" />
<script>
var b = {
a: []
}
(function()
{
b.a = [
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
]
})()
var a = function ( arg8 ){
var v1 = []
var v2 = arg8.length
var v0 = 0
while(v0 < v2){
var v3 = v0 + 1
var v4 = arg8[v0] & 255
if (v3 == v2)
{
v1.push (b.a[v4 >>> 2])
v1.push (b.a[( v4 & 3 ) << 4])
v1.push ("==")
}
else
{
var v5 = v3 + 1
v3 = arg8[v3] & 255
if (v5 == v2)
{
v1.push (b.a[v4 >>> 2])
v1.push (b.a[( v4 & 3 ) << 4 | ( v3 & 240 ) >>> 4])
v1.push (b.a[( v3 & 15 ) << 2])
v1.push ("=")
}
else
{
v0 = v5 + 1
v5 = arg8[v5] & 255
v1.push (b.a[v4 >>> 2])
v1.push (b.a[( v4 & 3 ) << 4 | ( v3 & 240 ) >>> 4])
v1.push (b.a[( v3 & 15 ) << 2 | ( v5 & 192 ) >>> 6])
v1.push (b.a[v5 & 63])
continue
}
}
break
}
return v1.join("")
}
console.log(a ([1,1]))
</script>
</head>
<body>
</body>
</html>