ASP+JS三级联动下拉菜单[调用数据库数据]

2018-09-06 13:03

阅读:301

  网上三级菜单多是多但是代码都比较烦,我这个应该说还是比较直观的:
肯定先要连接数据库了,不用说了
数据库结构
类别1表名称:a字段:ID,Name说明:ID为主键是类别1的ID值,Name为类别1的名称
类别2表名称:aa字段:ID,aID,Name说明:ID为主键是类别2的ID值,aID为所属类别1的ID值,Name为类别2的名称
类别3表名称:aaa字段:ID,aID,aaID,Name说明:ID为主键是类别3的ID值,aID为所属类别1的ID值,aaID为所属类别2的ID值,Name为类别3的名称
=====test1.asp你可以测试下,接收到的都是类别的ID值====
<%
response.write1:&Request.form(s1)&<BR>
response.write2:&Request.form(s2)&<BR>
response.write3:&Request.form(s3)&<BR>
%>
复制代码 代码如下:
<!--三级联动菜单开始-->
<scriptlanguage=JavaScript>
<!--
<%
二级数据保存到数组
Dimcount2,rsClass2,sqlClass2
setrsClass2=server.createobject(adodb.recordset)
sqlClass2=select*fromaa
rsClass2.opensqlClass2,conn,1,1
%>
varsubval2=newArray();
//数组结构:一级根值,二级根值,二级显示值
<%
count2=0
dowhilenotrsClass2.eof
%>
subval2[<%=count2%>]=newArray(<%=rsClass2(aID)%>,<%=rsClass2(ID)%>,<%=rsClass2(Name)%>)
<%
count2=count2+1
rsClass2.movenext
loop
rsClass2.close
%>
<%
三级数据保存到数组
Dimcount3,rsClass3,sqlClass3
setrsClass3=server.createobject(adodb.recordset)
sqlClass3=select*fromaaa
rsClass3.opensqlClass3,conn,1,1
%>
varsubval3=newArray();
//数组结构:二级根值,三级根值,三级显示值
<%
count3=0
dowhilenotrsClass3.eof
%>
subval3[<%=count3%>]=newArray(<%=rsClass3(aaID)%>,<%=rsClass3(ID)%>,<%=rsClass3(Name)%>)
<%
count3=count3+1
rsClass3.movenext
loop
rsClass3.close
%>
functionchangeselect1(locationid)
{
document.form1.s2.length=0;
document.form1.s2.options[0]=newOption(==请选择类别==,);
document.form1.s3.length=0;
document.form1.s3.options[0]=newOption(==请选择专题==,);
for(i=0;i<subval2.length;i++)
{
if(subval2[i][0]==locationid)
{document.form1.s2.options[document.form1.s2.length]=newOption(subval2[i][2],subval2[i][1]);}
}
}
functionchangeselect2(locationid)
{
document.form1.s3.length=0;
document.form1.s3.options[0]=newOption(==请选择专题==,);
for(i=0;i<subval3.length;i++)
{
if(subval3[i][0]==locationid)
{document.form1.s3.options[document.form1.s3.length]=newOption(subval3[i][2],subval3[i][1]);}
}
}
//-->
</script>
<formname=form1method=postaction=test1.asp>
三级联动:
<%
Dimcount1,rsClass1,sqlClass1
setrsClass1=server.createobject(adodb.recordset)
sqlClass1=select*froma
rsClass1.opensqlClass1,conn,1,1
%>
<selectname=s1onChange=changeselect1(this.value)>
<option>==请选择频道==</option>
<%
count1=0
dowhilenotrsClass1.eof
response.write<optionvalue=&rsClass1(ID)&>&rsClass1(Name)&</option>
count1=count1+1
rsClass1.movenext
loop
rsClass1.close
%>
</select>
<selectname=s2onChange=changeselect2(this.value)>
<option>==请选择类别==</option>
</select>
<selectname=s3>
<option>==请选择专题==</option>
</select>
<inputtype=submitname=Submitvalue=提交></form>
<!--三级联动菜单结束-->


评论


亲,登录后才可以留言!