ASP+JS三级联动下拉菜单[调用数据库数据]
程序员文章站
2023-12-01 16:45:34
网上三级菜单多是多但是代码都比较烦,我这个应该说还是比较直观的:'肯定先要连接数据库了,不用说了 '数据库结构 '类别1表名称:a 字段:id,nam...
网上三级菜单多是多但是代码都比较烦,我这个应该说还是比较直观的:
'肯定先要连接数据库了,不用说了
'数据库结构
'类别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.write"1:"&request.form("s1")&"<br>"
response.write"2:"&request.form("s2")&"<br>"
response.write"3:"&request.form("s3")&"<br>"
%>
<!-- 三级联动菜单 开始 -->
<script language="javascript">
<!--
<%
'二级数据保存到数组
dim count2,rsclass2,sqlclass2
set rsclass2=server.createobject("adodb.recordset")
sqlclass2="select * from aa"
rsclass2.open sqlclass2,conn,1,1
%>
var subval2 = new array();
//数组结构:一级根值,二级根值,二级显示值
<%
count2 = 0
do while not rsclass2.eof
%>
subval2[<%=count2%>] = new array('<%=rsclass2("aid")%>','<%=rsclass2("id")%>','<%=rsclass2("name")%>')
<%
count2 = count2 + 1
rsclass2.movenext
loop
rsclass2.close
%>
<%
'三级数据保存到数组
dim count3,rsclass3,sqlclass3
set rsclass3=server.createobject("adodb.recordset")
sqlclass3="select * from aaa"
rsclass3.open sqlclass3,conn,1,1
%>
var subval3 = new array();
//数组结构:二级根值,三级根值,三级显示值
<%
count3 = 0
do while not rsclass3.eof
%>
subval3[<%=count3%>] = new array('<%=rsclass3("aaid")%>','<%=rsclass3("id")%>','<%=rsclass3("name")%>')
<%
count3 = count3 + 1
rsclass3.movenext
loop
rsclass3.close
%>
function changeselect1(locationid)
{
document.form1.s2.length = 0;
document.form1.s2.options[0] = new option('==请选择类别==','');
document.form1.s3.length = 0;
document.form1.s3.options[0] = new option('==请选择专题==','');
for (i=0; i<subval2.length; i++)
{
if (subval2[i][0] == locationid)
{document.form1.s2.options[document.form1.s2.length] = new option(subval2[i][2],subval2[i][1]);}
}
}
function changeselect2(locationid)
{
document.form1.s3.length = 0;
document.form1.s3.options[0] = new option('==请选择专题==','');
for (i=0; i<subval3.length; i++)
{
if (subval3[i][0] == locationid)
{document.form1.s3.options[document.form1.s3.length] = new option(subval3[i][2],subval3[i][1]);}
}
}
//-->
</script>
<form name="form1" method="post" action="test1.asp">
三级联动:
<%
dim count1,rsclass1,sqlclass1
set rsclass1=server.createobject("adodb.recordset")
sqlclass1="select * from a"
rsclass1.open sqlclass1,conn,1,1
%>
<select name="s1" onchange="changeselect1(this.value)">
<option>==请选择频道==</option>
<%
count1 = 0
do while not rsclass1.eof
response.write"<option value="&rsclass1("id")&">"&rsclass1("name")&"</option>"
count1 = count1 + 1
rsclass1.movenext
loop
rsclass1.close
%>
</select>
<select name="s2" onchange="changeselect2(this.value)">
<option>==请选择类别==</option>
</select>
<select name="s3">
<option>==请选择专题==</option>
</select>
<input type="submit" name="submit" value="提交"></form>
<!-- 三级联动菜单 结束 -->
'肯定先要连接数据库了,不用说了
'数据库结构
'类别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.write"1:"&request.form("s1")&"<br>"
response.write"2:"&request.form("s2")&"<br>"
response.write"3:"&request.form("s3")&"<br>"
%>
复制代码 代码如下:
<!-- 三级联动菜单 开始 -->
<script language="javascript">
<!--
<%
'二级数据保存到数组
dim count2,rsclass2,sqlclass2
set rsclass2=server.createobject("adodb.recordset")
sqlclass2="select * from aa"
rsclass2.open sqlclass2,conn,1,1
%>
var subval2 = new array();
//数组结构:一级根值,二级根值,二级显示值
<%
count2 = 0
do while not rsclass2.eof
%>
subval2[<%=count2%>] = new array('<%=rsclass2("aid")%>','<%=rsclass2("id")%>','<%=rsclass2("name")%>')
<%
count2 = count2 + 1
rsclass2.movenext
loop
rsclass2.close
%>
<%
'三级数据保存到数组
dim count3,rsclass3,sqlclass3
set rsclass3=server.createobject("adodb.recordset")
sqlclass3="select * from aaa"
rsclass3.open sqlclass3,conn,1,1
%>
var subval3 = new array();
//数组结构:二级根值,三级根值,三级显示值
<%
count3 = 0
do while not rsclass3.eof
%>
subval3[<%=count3%>] = new array('<%=rsclass3("aaid")%>','<%=rsclass3("id")%>','<%=rsclass3("name")%>')
<%
count3 = count3 + 1
rsclass3.movenext
loop
rsclass3.close
%>
function changeselect1(locationid)
{
document.form1.s2.length = 0;
document.form1.s2.options[0] = new option('==请选择类别==','');
document.form1.s3.length = 0;
document.form1.s3.options[0] = new option('==请选择专题==','');
for (i=0; i<subval2.length; i++)
{
if (subval2[i][0] == locationid)
{document.form1.s2.options[document.form1.s2.length] = new option(subval2[i][2],subval2[i][1]);}
}
}
function changeselect2(locationid)
{
document.form1.s3.length = 0;
document.form1.s3.options[0] = new option('==请选择专题==','');
for (i=0; i<subval3.length; i++)
{
if (subval3[i][0] == locationid)
{document.form1.s3.options[document.form1.s3.length] = new option(subval3[i][2],subval3[i][1]);}
}
}
//-->
</script>
<form name="form1" method="post" action="test1.asp">
三级联动:
<%
dim count1,rsclass1,sqlclass1
set rsclass1=server.createobject("adodb.recordset")
sqlclass1="select * from a"
rsclass1.open sqlclass1,conn,1,1
%>
<select name="s1" onchange="changeselect1(this.value)">
<option>==请选择频道==</option>
<%
count1 = 0
do while not rsclass1.eof
response.write"<option value="&rsclass1("id")&">"&rsclass1("name")&"</option>"
count1 = count1 + 1
rsclass1.movenext
loop
rsclass1.close
%>
</select>
<select name="s2" onchange="changeselect2(this.value)">
<option>==请选择类别==</option>
</select>
<select name="s3">
<option>==请选择专题==</option>
</select>
<input type="submit" name="submit" value="提交"></form>
<!-- 三级联动菜单 结束 -->
上一篇: 用存储过程、GetRows()、抽取10万条数据的速度测试
下一篇: 使用asp代码突破图片的防盗连