總共三級,通過學院department選擇系division,通過系選擇專業speciality
<%@ page language="java" import="java.util.*,java.lang.*,java.sql.*" pageEncoding="GB18030"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script language="javascript">
var DeparmentCount; //三級聯動菜單代碼
DeparmentCount = 0;
var DivisionCount;
DivisionCount = 0;
var SpecialityCount;
SpecialityCount = 0
Department = new Array();
Division = new Array();
Speciality = new Array();
<%
system.pub.oraclesql myDao = new system.pub.oraclesql();
ResultSet rs = myDao.execQuery("select * from division");
int i=0;
for(rs.next();!rs.isAfterLast();rs.next(),i++){%>
Division[<%=i%>] = new Array("<%=rs.getString("Division_name")%>","<%=rs.getString("department_id")%>","<%=rs.getString("id")%>");
<% }%>
DivisionCount = <%=i%>;
<%
System.out.println("debug....1");
rs = myDao.execQuery("select * from speciality");
i=0;
for(rs.next();!rs.isAfterLast();rs.next(),i++){%>
Speciality[<%=i%>] = new Array("<%=rs.getString("Speciality_name")%>","<%=rs.getString("division_id")%>","<%=rs.getString("id")%>");
<% }%>
SpecialityCount = <%=i%>;
function changeDepartment(DepartmentId){
document.form1.Switch.value="false";
document.form1.division.length = 0;
var DepartmentId = DepartmentId;
var i;
for (i=0;i < DivisionCount; i++){
if (Division[i][1] == DepartmentId){
document.form1.division.options[document.form1.division.length] = new Option(Division[i][0], Division[i][2]);
}
}
changeDivision(document.form1.division.value);
}
function changeDivision(DivisionId){
document.form1.Switch.value="false";
document.form1.speciality.length = 0;
var DivisionId = DivisionId;
var i;
for (i=0;i < SpecialityCount; i++){
if (Speciality[i][1] == DivisionId){
document.form1.speciality.options[document.form1.speciality.length] = new Option(Speciality[i][0], Speciality[i][2]);
}
}
if(document.form1.speciality.length!=0){
document.form1.speciality.style.visibility = 'visible';
}
else{
document.form1.speciality.style.visibility = 'hidden';
}
}
function initialSelect(){
changeDepartment(document.form1.department.options[document.form1.department.selectedIndex].value);
}
</script>
</head>
<body onload="initialSelect();">
<form name="form1">
<select name="department" size="1" id="department" style="width:20%" onChange="changeDepartment(document.form1.department.options[document.form1.department.selectedIndex].value)">
<%
rs = myDao.execQuery("select * from department");
i=0;
for(rs.next();!rs.isAfterLast();rs.next(),i++){%>
<option value="<%=rs.getString("id")%>"><%=rs.getString("Department_name")%></option>
<% }%>
</select>
<select name="division" size="1" id="division" onChange="changeDivision(document.form1.division.options[document.form1.division.selectedIndex].value)">
</select>
<input type="hidden" name="Switch" value="false">
<select name="speciality" size="1">
</select>
</form>
</body>
</html>
實現之後,從資料庫裡面讀三個表裡的資訊,產生的html代碼如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script language="javascript">
var DeparmentCount; //三級聯動菜單代碼
DeparmentCount = 0;
var DivisionCount;
DivisionCount = 0;
var SpecialityCount;
SpecialityCount = 0
Department = new Array();
Division = new Array();
Speciality = new Array();
Division[0] = new Array("ccddd","13","100");
Division[1] = new Array("cc","14","101");
Division[2] = new Array("cczz","14","102");
Division[3] = new Array("sdfgsdfg","14","103");
Division[4] = new Array("asdasdf","14","104");
Division[5] = new Array("zxcvzxcv","14","106");
Division[6] = new Array("zxcvzv","2","107");
Division[7] = new Array("zxcvzxcv","13","108");
Division[8] = new Array("電視新聞系","2","4");
Division[9] = new Array("電視攝影系","2","5");
Division[10] = new Array("教研室","3","6");
Division[11] = new Array("機房","3","7");
Division[12] = new Array("辦公室","3","8");
Division[13] = new Array("zxcv","14","105");
Division[14] = new Array("通訊工程系","1","1");
Division[15] = new Array("電子資訊工程系","1","2");
Division[16] = new Array("科學藝術系","1","3");
Division[17] = new Array("aaaa","15","109");
DivisionCount = 18;
Speciality[0] = new Array("ertwert","101","100");
Speciality[1] = new Array("vzxcvzxcv","100","101");
Speciality[2] = new Array("zzz","100","102");
Speciality[3] = new Array("asdf","100","103");
Speciality[4] = new Array("新聞評論","4","1");
Speciality[5] = new Array("網路新聞","4","2");
Speciality[6] = new Array("多媒體技術","1","3");
Speciality[7] = new Array("藝術攝影","5","4");
Speciality[8] = new Array("kkkk","109","104");
SpecialityCount = 9;
function changeDepartment(DepartmentId){
document.form1.Switch.value="false";
document.form1.division.length = 0;
var DepartmentId = DepartmentId;
var i;
for (i=0;i < DivisionCount; i++){
if (Division[i][1] == DepartmentId){
document.form1.division.options[document.form1.division.length] = new Option(Division[i][0], Division[i][2]);
}
}
changeDivision(document.form1.division.value);
}
function changeDivision(DivisionId){
document.form1.Switch.value="false";
document.form1.speciality.length = 0;
var DivisionId = DivisionId;
var i;
for (i=0;i < SpecialityCount; i++){
if (Speciality[i][1] == DivisionId){
document.form1.speciality.options[document.form1.speciality.length] = new Option(Speciality[i][0], Speciality[i][2]);
}
}
if(document.form1.speciality.length!=0){
document.form1.speciality.style.visibility = 'visible';
}
else{
document.form1.speciality.style.visibility = 'hidden';
}
}
function initialSelect(){
changeDepartment(document.form1.department.options[document.form1.department.selectedIndex].value);
}
</script>
</head>
<body onload="initialSelect();">
<form name="form1">
<select name="department" size="1" id="department" style="width:20%" onChange="changeDepartment(document.form1.department.options[document.form1.department.selectedIndex].value)">
<option value="2">電視與新聞學院</option>
<option value="3">計算中心</option>
<option value="1">工程學院</option>
<option value="13">fff</option>
<option value="14">gdsf</option>
<option value="15">zxcef</option>
</select>
<select name="division" size="1" id="division" onChange="changeDivision(document.form1.division.options[document.form1.division.selectedIndex].value)">
</select>
<input type="hidden" name="Switch" value="false">
<select name="speciality" size="1">
</select>
</form>
</body>
</html>