在一个jsp页面实现二级下拉框联动,实时读取数据库数据,这个方法非常使用,只需要修改很小的地方就可以使用。设计的文件,serch.jsp,main.js,bytetostr.js,
先讲一下main.js,这是javascript,其中注意修改jsp页面名称。
function findObject(fName,initValue)...{
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.open("POST","searchmx.jsp?findObject="+fName+"&initValue="+initValue,false);//注意修改jsp页面
xmlhttp.send();
document.getElementById(fName).innerHTML=bytes2BSTR(xmlhttp.responsebody); //bytes2BSTR函数在bytetostr.js中
}
其次为bytetostr.js,这是vbscript,这个不需要修改任何内容,他的主要功能就是读取数据转换字符串用的。
Function bytes2BSTR(vIn)
dim i
strReturn = ""
For i = 1 To LenB(vIn)
ThisCharCode = AscB(MidB(vIn,i,1))
If ThisCharCode < &H80 Then
strReturn = strReturn & Chr(ThisCharCode)
Else
NextCharCode = AscB(MidB(vIn,i+1,1))
strReturn = strReturn & Chr(CLng(ThisCharCode) * &H100 + CInt(NextCharCode))
i = i + 1
End If
Next
bytes2BSTR = strReturn
End Function
接下来就是serch.jsp页面,只需要修改div中的id为你想要的名字,再将一开始的包更换一下,已经sql语句改下就可以了,有不明白的可以参与讨论
<%@ page contentType="text/html; charset=gb2312" language="java" import="com.sjth.zdsygl.vo.*,com.sjth.zdsygl.biz.*,java.util.*,com.sjth.zdsygl.jdbc.*,java.sql.ResultSet"%>//导入相应的包
<%
//这部分代码用于执行查询数据库,返回字符串
if (request.getParameter("findObject")!=null)...{
if (request.getParameter("findObject").equals("hy_dm"))...{
DBConnect conn = null;
ResultSet rs = null;
try...{
out.print("<select name='hy_dm' onchange="javascript:findObject('hymx_dm',this.value)">");
String sql = "select * from dm_hy group by left(hy_dm,7)";
conn = new DBConnect();
conn.setPstmt(sql);
rs = conn.executeQuery(sql);
while(rs.next())...{
out.print("<option value='"+rs.getString("hy_dm")+"'>"+rs.getString("hy_mc")+"</option>");
}
out.print("</select>");
}
catch (Exception e)...{
}
finally...{
try...{
if (rs != null)
rs.close();
if (conn != null)
conn.close();
}
catch (Exception e)...{
e.printStackTrace();
}
}
}
if (request.getParameter("findObject").equals("hymx_dm"))...{
DBConnect conn = null;
ResultSet rs = null;
try...{
out.print("<select name='hymx_dm' >");
String sql = null;
if (request.getParameter("initValue").equals(""))...{
sql = "select * from dm_hy";
}
else...{
sql = "select * from dm_hy where hy_dm like '"+request.getParameter("initValue")+"%'";
}
conn = new DBConnect();
conn.setPstmt(sql);
rs = conn.executeQuery(sql);
while(rs.next())...{
out.print("<option value='"+rs.getString("hy_dm")+"'>"+rs.getString("hy_mc")+"</option>");
}
out.print("</select>");
}
catch (Exception e)...{
}
finally...{
try...{
if (rs != null)
rs.close();
if (conn != null)
conn.close();
}
catch (Exception e)...{
e.printStackTrace();
}
}
}
return;
}
%>
<html>
<head>
<link href="CSS/table.css" type="text/css" rel="stylesheet">
<script language=vbscript src="css/bytetostr.js"></script>
<script language=javascript src="css/main.js"></script>//导入两个js文件
</head>
<body>
<div>所属行业:</div>
<div id="hy_dm"></div> //用来显示返回的字符串
<div >所属明细行业:</div>
<div id="hymx_dm"></div>//用来显示返回的字符串
</body>
</html>
<script language="javascript">
findObject("hy_dm","");
findObject("hymx_dm","");//这两条是页面执行是调用
</script>
欢迎大家讨论