测试数据库:test.mdb,你可以自己建一个。
表1:big_class
字段:big_class_id(自动编号),big_class_name(文本)
表2:small_class
字段:small_class_id(自动编号),small_class_name(文本),belongto_big_class(数字)
以下是程序清单,共1个文件,文件名:test.asp
<% 'option explicit dim conn,connstr,db db="test.mdb" '测试数据库 Set conn = Server.CreateObject("ADODB.Connection") connstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath(""&db&"") conn.Open connstr %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>ASP+JavaScript+数据库 级联下拉菜单</title> <style> table{border: #00215a 1px dashed;} td{font-size:12px;} input,select{ font-size:9pt; border-style:solid; border-width:1; cursor:default; color:#03326B; background-color:#FFFFFF; height:16px; } a:link,a:visited{color: #000000;text-decoration: none;} a:hover {color: #ff0000;text-decoration: none;FILTER: glow(color=ffffff,strength=0) shadow(color=aaaaaa,direction:135); POSITION: relative; WIDTH: 100%;} </style> <script language="JavaScript"> function addbig(){ document.all.a.style.display="block"; document.all.b.style.display="none"; document.all.c.style.display="none"; } function addsmall(){ document.all.b.style.display="block"; document.all.a.style.display="none"; document.all.c.style.display="none"; } function viewmenu(){ document.all.a.style.display="none"; document.all.b.style.display="none"; document.all.c.style.display="block"; } </script> <% '从小类表中取出数据 set rs=server.CreateObject("adodb.recordset") sql="select * from small_class " rs.open sql,conn,1,1 %> <script language="JavaScript"> var num; //定义数组 var calArray=new Array(); <% dim j j=0 do while not rs.eof %> //将小类表中的所有相关记录存到数组calArray的对应元素中。 calArray[<%=j%>]=new Array("<%=rs("small_class_id")%>","<%=rs("small_class_name")%>","<%=rs("belongto_big_class")%>"); <% j=j+1 rs.movenext loop rs.close set rs=nothing %> //给num赋值为记录总数 num=<%=j%> function givevalue(myvalue){ /*当选择大类列表的值不为空时首先清空小类下拉列表的所有项目。不然小类列表中的项目会叠加的。 同时也是初始化 options 的值为0 */ document.form3.small_class_select.length = 0; //循环写出请求的大类所对应的小类。 for (i=0;i < num; i++) { if (calArray[i][2] == myvalue) { document.form3.small_class_select.options[document.form3.small_class_select.length] = new Option(calArray[i][1], calArray[i][0],"",""); /*定义新的Option对象并赋值。options的索引值从0开始。new Option对象有4个属性,对应分别是:文本串、value、defaultSelect、selected。在这里只用了第一个和第二个。*/ } } } </script> <script language="JavaScript"> function chk1(){ if (form1.big_class_name.value=="") { alert("请输入大类名称!"); form1.big_class_name.focus(); return false; } } function chk2(){ if (form2.small_class_name.value=="") { alert("请输入小类名称!"); form2.small_class_name.focus(); return false; } } </script> </head> <body background="background.jpg"> <% dim rs,sql,noclass select case request("action") case "addbigclass" addbigclass case "addsmallclass" addsmallclass end select %> <div align="center"></div> <table width="60%" border="0" align="center" cellpadding="0" cellspacing="0"> <tr valign="middle"> <td height="48" colspan="3"> <div align="center"><font color="#999900"><b><font color="#FF0000">ASP</font>+<font color="#FF0000">JavaScript</font>+<font color="#FF0000">数据库</font> 级联下拉菜单</b></font></div></td> </tr> <tr> <td width="22%" height="21" valign="bottom"><a href="#" onClick="addbig()">添加大类</a> </td> <td width="22%" valign="bottom"><a href="#" onClick="addsmall()">添加小类</a></td> <td width="56%" valign="bottom"><a href="#" onClick="viewmenu()">预览效果</a></td> </tr> <tr> <td height="33" colspan="3" valign="top"> <hr align="left" width="60%" size="1" color="#999900"> </td> </tr> <tr> <td colspan="3" valign="top"> <div id="a" style="display:none"> <form name="form1" method="post" action="?action=addbigclass" onSubmit="return chk1()"> 大类名称: <input name="big_class_name" type="text" id="big_class_name" size="16"> <input type="submit" name="Submit" value=" 添 加 "> </form> </div> <div id="b" style="display:none"> <form name="form2" method="post" action="?action=addsmallclass" onSubmit="return chk2()"> 选择大类后添加小类: <select name="addselect"> <% set rs=server.CreateObject("adodb.recordset") sql="big_class" rs.open sql,conn,1,1 if rs.eof or rs.bof then %> <option selected>还没有添加大类</option> <% else do while not rs.eof %> <option value="<%=rs("big_class_id")%>"><%=trim(rs("big_class_name"))%></option> <% rs.movenext loop end if rs.close set rs=nothing %> </select> 小类名称: <input name="small_class_name" type="text" id="small_class_name" size="16"> <input type="submit" name="Submit2" value=" 添 加 "> </form> </div> <div id="c" style="display:block"> <form name="form3" method="post" action=""> <select name="big_class_select" onChange="givevalue(document.form3.big_class_select.options[document.form3.big_class_select.selectedIndex].value)"> <% dim firstid set rs=server.CreateObject("adodb.recordset") sql="select * from big_class order by big_class_id" rs.open sql,conn,1,1 if rs.eof or rs.bof then noclass=1 %> <option selected>还没有添加大类</option> <% else rs.movefirst firstid=rs("big_class_id") '在没有选择大类(页面刚载入)时要载入的大类。 %> <option value="<%=rs("big_class_id")%>" selected><%=trim(rs("big_class_name"))%></option> <% rs.movenext do while not rs.eof %> <option value="<%=rs("big_class_id")%>"><%=trim(rs("big_class_name"))%></option> <% rs.movenext loop end if rs.close set rs=nothing %> </select> <select name="small_class_select"> <%if noclass=1 then%> <option value="" selected>没有小类</option> <% else '在没有选择大类(页面刚载入)时要载入的小类,要跟默认的大类对应。 set rs=server.CreateObject("adodb.recordset") sql="select * from small_class where belongto_big_class="&firstid rs.open sql,conn,1,1 if rs.eof or rs.bof then %> <option value="" selected>没有小类</option> <% else do while not rs.eof %> <option value="<%=rs("small_class_id")%>"><%=trim(rs("small_class_name"))%></option> <% rs.movenext loop end if rs.close set rs=nothing end if %> </select> </form> </div></td> </tr> </table> <% sub addbigclass() set rs=server.CreateObject("adodb.recordset") sql="select * from big_class where big_class_name='"&trim(request("big_class_name"))&"'" rs.open sql,conn,1,3 if not(rs.eof and rs.bof) then response.Write("<script>alert('该大类已经存在!');</script>") else rs.addnew rs("big_class_name")=trim(request("big_class_name")) rs.update response.Write("<script>alert('大类添加成功!');self.location='test.asp?action=viewmenu';</script>") end if rs.close set rs=nothing end sub sub addsmallclass() set rs=server.CreateObject("adodb.recordset") sql="select * from small_class where small_class_name='"&trim(request("small_class_name"))&"' and belongto_big_class="&request("addselect") rs.open sql,conn,1,3 if not(rs.eof and rs.bof) then response.Write("<script>alert('该小类已经存在!');</script>") else rs.addnew rs("belongto_big_class")=request("addselect") rs("small_class_name")=trim(request("small_class_name")) rs.update response.Write("<script>alert('小类添加成功!');self.location='test.asp?action=viewmenu';</script>") end if rs.close set rs=nothing end sub conn.close set conn=nothing %> </body> </html>