js| Dropdown | page
In a JSP page to achieve a two-level drop-down box linkage, real-time reading of database data.
In a JSP page to achieve a two-level drop-down box linkage, real-time reading database data, this method is very useful, only need to modify a small place can be used. The design of the document, Serch.jsp,main.js,bytetostr.js,
First, let's talk about Main.js, which is JavaScript, which takes note of modifying the JSP page name.
function Findobject (fname,initvalue) ... {
var xmlhttp = new ActiveXObject ("Microsoft.XMLHTTP");
Xmlhttp.open ("POST", "searchmx.jsp?findobject=" +fname+ "&initvalue=" +initvalue,false);/Note Modify JSP page
Xmlhttp.send ();
document.getElementById (FName). INNERHTML=BYTES2BSTR (Xmlhttp.responsebody); Bytes2bstr function in Bytetostr.js
}
followed by Bytetostr.js, which is VBScript, which does not need to modify any content, and his main function is to read the data conversion string.
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
Next is the serch.jsp page, only need to modify the div in the ID for the name you want, and then the beginning of the package to replace, has been changed SQL statements can be, there is no understanding can participate in the discussion
<%@ 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 "%>//Import the appropriate package
<%
This part of the code is used to execute the query database and return the string
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 ' >");
String sql = "SELECT * from Dm_hy group to 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
}
%>
<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>//import two JS files
<body>
<div> Industry:</div>
<div id= "HY_DM" ></div>//used to display the returned string
<div > Affiliated Industry:</div>
<div id= "HYMX_DM" ></div>//used to display the returned string
</body>
<script language= "JavaScript" >
Findobject ("Hy_dm", "");
Findobject ("Hymx_dm", "");//These two are page execution is called
</script>