有什么好办法可以在jsp页面里把一个java的数组附值给javascript的数组?
3个回答
展开全部
这是一个级联选择程序,从数据库中读取数据,然后传到javascript数组中,实际上是在java中构造javascript语句,希望对你有所帮助,代码比较多,看对你有用的部分。
<%@ page contentType="text/html;charset=utf-8"%>
<%@ page import="java.sql.*"%>
<html>
<HTML>
<HEAD>
<TITLE> </TITLE>
</HEAD>
<BODY>
<%
Connection conn = null;
ResultSet res = null;
ResultSet res1 = null;
String js_block="";
String cate_block="";
java.sql.PreparedStatement prepar = null;
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
conn = DriverManager
.getConnection(
"jdbc:microsoft:sqlserver://localhost:1433;databasename=test",
"sa", "123456");
//out.println("连接成功");
} catch (SQLException ex) {
out.println(ex.getMessage() + "路径错误");
} catch (ClassNotFoundException ex) {
out.println(ex.getMessage() + "驱动错误");
}
String sql1="select * from cate";
prepar=conn.prepareStatement(sql1);
res=prepar.executeQuery();
while(res.next())
{
cate_block+="<option value=\""+res.getInt("cateid")+"\" selected>"+res.getString("catename")+"</option>\n";
}
String sql="select cate.catename,cate.cateid,classname,class.classid from cate,cate_class,class where cate.cateid=cate_class.cateid and cate_class.classid=class.classid";
prepar=conn.prepareStatement(sql);
res=prepar.executeQuery();
int i=0;
while(res.next())
{
//out.print(res.getString(1)+"-->"+res.getString(3)+"<br>");
js_block+="subcat["+i+"]=new Array(\""+res.getString("classname")+"\",\""+res.getInt("cateid")+"\",\""+res.getInt("classid")+"\");\n";
i++;
}
js_block+="onecount="+i+";";
res.close();
conn.close();
%>
<script language = "JavaScript">
var onecount;
onecount=0;
subcat = new Array();
<%=js_block%> //此处定义数组subcat并初始化(在jsp页面中被初始化)
//-------------------------------------------------
// END
//-------------------------------------------------
//document.write("测试"+"sdaa"+subcat[0][1])
function changelocation(locationid)
{
document.iDownS.soft_class_id.length = 0;
var locationid=locationid;
var i;
//document.write(locationid);
for (i=0;i < onecount; i++)
{
if (subcat[i][1] == locationid)
{
//document.write("yes,");
document.iDownS.soft_class_id.options[document.iDownS.soft_class_id.length] = new Option(subcat[i][0], subcat[i][2]);
}
}
}
</script>
<form action="content.php" method="post" name="iDownS" ENCTYPE="multipart/form-data" >
<table width="90%" border="0" cellpadding="5" cellspacing="1" bgcolor="#000000">
<tr bgcolor="#F8F8F8">
<td width="120">
<font color="#CC0000">*</font>选择分类:
</td>
<td>
<SELECT NAME="soft_category_id" onChange="changelocation(document.iDownS.soft_category_id.options[document.iDownS.soft_category_id.selectedIndex].value)">
<option selected value="1">[Select Cate]</option>
<%=cate_block%>
</SELECT>
=>
<SELECT NAME="soft_class_id">
<option selected value="1">[Select class]</option>
</SELECT>
</td>
</tr>
</table>
</form>
</BODY>
</HTML>
<%@ page contentType="text/html;charset=utf-8"%>
<%@ page import="java.sql.*"%>
<html>
<HTML>
<HEAD>
<TITLE> </TITLE>
</HEAD>
<BODY>
<%
Connection conn = null;
ResultSet res = null;
ResultSet res1 = null;
String js_block="";
String cate_block="";
java.sql.PreparedStatement prepar = null;
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
conn = DriverManager
.getConnection(
"jdbc:microsoft:sqlserver://localhost:1433;databasename=test",
"sa", "123456");
//out.println("连接成功");
} catch (SQLException ex) {
out.println(ex.getMessage() + "路径错误");
} catch (ClassNotFoundException ex) {
out.println(ex.getMessage() + "驱动错误");
}
String sql1="select * from cate";
prepar=conn.prepareStatement(sql1);
res=prepar.executeQuery();
while(res.next())
{
cate_block+="<option value=\""+res.getInt("cateid")+"\" selected>"+res.getString("catename")+"</option>\n";
}
String sql="select cate.catename,cate.cateid,classname,class.classid from cate,cate_class,class where cate.cateid=cate_class.cateid and cate_class.classid=class.classid";
prepar=conn.prepareStatement(sql);
res=prepar.executeQuery();
int i=0;
while(res.next())
{
//out.print(res.getString(1)+"-->"+res.getString(3)+"<br>");
js_block+="subcat["+i+"]=new Array(\""+res.getString("classname")+"\",\""+res.getInt("cateid")+"\",\""+res.getInt("classid")+"\");\n";
i++;
}
js_block+="onecount="+i+";";
res.close();
conn.close();
%>
<script language = "JavaScript">
var onecount;
onecount=0;
subcat = new Array();
<%=js_block%> //此处定义数组subcat并初始化(在jsp页面中被初始化)
//-------------------------------------------------
// END
//-------------------------------------------------
//document.write("测试"+"sdaa"+subcat[0][1])
function changelocation(locationid)
{
document.iDownS.soft_class_id.length = 0;
var locationid=locationid;
var i;
//document.write(locationid);
for (i=0;i < onecount; i++)
{
if (subcat[i][1] == locationid)
{
//document.write("yes,");
document.iDownS.soft_class_id.options[document.iDownS.soft_class_id.length] = new Option(subcat[i][0], subcat[i][2]);
}
}
}
</script>
<form action="content.php" method="post" name="iDownS" ENCTYPE="multipart/form-data" >
<table width="90%" border="0" cellpadding="5" cellspacing="1" bgcolor="#000000">
<tr bgcolor="#F8F8F8">
<td width="120">
<font color="#CC0000">*</font>选择分类:
</td>
<td>
<SELECT NAME="soft_category_id" onChange="changelocation(document.iDownS.soft_category_id.options[document.iDownS.soft_category_id.selectedIndex].value)">
<option selected value="1">[Select Cate]</option>
<%=cate_block%>
</SELECT>
=>
<SELECT NAME="soft_class_id">
<option selected value="1">[Select class]</option>
</SELECT>
</td>
</tr>
</table>
</form>
</BODY>
</HTML>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询