在一个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中 }
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>
|
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有