javascript级联菜单,数据从数据库中获取

本文介绍了一个用于录入教学班信息的网页表单及其实现的部门、专业等多级联动选择功能。通过Java后端与JavaScript前端相结合的方式,实现了下拉列表的动态更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1
1.html代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<%@ page contentType="text/html; charset=gb2312" %>
<%@ page import="java.sql.*" %>
 
<html>
<head>
<!--
放置javascript代码
-->
</head>
 
<body style="font-size: 12px;" bgcolor="eeeeee">
<form name="form1" action="SpecialityRelation_Insert.jsp" method="get">
<img onclick="history.go(-1);" src="../../images/back_blue.gif" onmousemove="this.src='../../images/back_red.gif';" onmouseout="this.src='../../images/back_blue.gif'" style="cursor:hand;">
<hr>
 
<%
    String logicalclass_name = request.getParameter("name");
    String logicalclass_id = request.getParameter("logicalclass_id");
%>
 
<input name="logicalclass_id" value="<%=logicalclass_id%>" type="hidden">
<table border="1px">
<tr>
<td width="100px">教学班名称</td>
<td width="300px">
<input class="MyInputBox" name="name" value="<%=logicalclass_name%>" style="background:'#dddddd';" size="50" readonly/>
</td>
</tr>
<tr>
<td>所在专业</td>
<td>
<select class="MySelect" name="department" size="1" id="department" 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 class="MySelect" 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 class="MySelect" name="speciality" id="speciality" size="1" onChange="changeSpeciality(document.form1.speciality.options[document.form1.speciality.selectedIndex].value)"></select>
 
<select class="MySelect" name="Grade" id="Grade" size="1" onChange="changeGrade(document.form1.Grade.options[document.form1.Grade.selectedIndex].value)">
</select>
 
<select class="MySelect" name="ObjectID" size="1">
</select>
 
</td>
</tr>
 
 
<tr>
<td>说明</td>
<td><textarea name="other" class="MyTextArea" name="other" cols="48" rows="10"></textarea></td>
</tr>
 
<tr>
<td colspan="2" align="right">
<img src="../../images/submit_blue.gif" onmousemove="this.src='../../images/submit_red.gif';" onmouseout="this.src='../../images/submit_blue.gif'" style="cursor:hand;" onclick="document.form1.submit();"
</td>
</tr>
</table>
</form>
</body>
</html>

  

1
<br>

 2.javascript代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
2.javascript代码
<script language="javascript">
 
var DeparmentCount; //三级联动菜单代码
DeparmentCount = 0;
var DivisionCount;
DivisionCount = 0;
var SpecialityCount;
SpecialityCount = 0;
 
//GradeCount = 0;
ObjectIDCount = 0;
Department = new Array();
Division = new Array();
Speciality = new Array();
//Grade = new Array();
ObjectID = 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")%>");
 
<% 
}//for
%>
         
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")%>");
 
<% 
}//for
%>
 SpecialityCount = <%=i%>;
         
<%
    rs = myDao.execQuery("select distinct speciality_id,grade,object_id,name from student t,object o where o.id= t.object_id");
    i=0;
    for(rs.next();!rs.isAfterLast();rs.next(),i++)
    {
%>
 
 ObjectID[<%=i%>] = new Array("<%=rs.getString("speciality_id")%>","<%=rs.getString("grade")%>","<%=rs.getString("object_id")%>","<%=rs.getString("name")%>");
 
<% 
}//for
%>
             
    ObjectIDCount = <%=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);
            if(document.form1.division.length!=0)
            {
                document.form1.division.style.visibility = 'visible';
            }
            else
            {
                document.form1.division.style.visibility = 'hidden';
            
             
        }
         
        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';
                document.form1.Grade.style.visibility = 'hidden';
                document.form1.ObjectID.style.visibility = 'hidden';
                 
            }
            changeSpeciality(document.form1.speciality.value);
        }
         
        function changeSpeciality(SpecialityID)
        {
            document.form1.Switch.value="false";
            document.form1.Grade.length = 0;
            var SpecialityID = SpecialityID;
            var i,j;
            var flag = 0;
            for (i=0;i < ObjectIDCount; i++)
            {
                if(ObjectID[i][0] == SpecialityID)
                {
                    for(j=0,flag=0;j<document.form1.Grade.length;j++)
                    {
                        if(document.form1.Grade.options[j].value==ObjectID[i][1])
                        {
                            flag = 1;
                            break;
                        }
                    }
                    if(flag!=1)
                    {
                        document.form1.Grade.options[document.form1.Grade.length] = new Option(ObjectID[i][1], ObjectID[i][1]);
                    }
                }       
            }
            if(document.form1.Grade.length!=0)
            {
                document.form1.Grade.style.visibility = 'visible';
            }
            else
            
                document.form1.Grade.style.visibility = 'hidden';
                document.form1.ObjectID.style.visibility ='hidden';
            }
            changeGrade(document.form1.Grade.value);
        }
         
        function changeGrade(GradeID)
        {
            document.form1.Switch.value="false";
            document.form1.ObjectID.length = 0;
            var GradeID = GradeID;
            var i;
            for (i=0;i < ObjectIDCount; i++)
            {
                if(ObjectID[i][1] == GradeID && ObjectID[i][0] == document.form1.speciality.value)
                {
                    document.form1.ObjectID.options[document.form1.ObjectID.length] = new Option(ObjectID[i][3],ObjectID[i][2]);
                }       
            }
            if(document.form1.ObjectID.length!=0)
            {
                document.form1.ObjectID.style.visibility = 'visible';
            }
            else
            
                document.form1.ObjectID.style.visibility = 'hidden';
            }
        }
         
         
        function  initialSelect()
        {
            changeDepartment(document.form1.department.options[document.form1.department.selectedIndex].value);
        }
         
         
        </script>

  

 本文转自二郎三郎博客园博客,原文链接:http://www.cnblogs.com/haore147/p/3617944.html,如需转载请自行联系原作者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值