ASP+JS三级连动下拉框

本文介绍了一种使用JavaScript实现的三级联动下拉菜单的方法。通过动态生成选项并响应选择变化,实现了一级、二级和三级分类之间的联动效果。

<%
'OK工作室 http://ok123.k989.com
'sort   一级分类表:sort_id,sort_name
'nsort  二级分类表:nsort_id,sort_id,nsort_name
'nnsort 三级分类表:nnsort_id,nsort_id,nnsort_name
%>
<!--#include file="conn.asp"-->
<%
dim count
set rs=server.createobject("adodb.recordset")
sql="select * from  nsort"
rs.open sql,conn,1,3
%>
<script language = "JavaScript">
var onecount;
onecount=0;
subcat = new Array();
        <%
        count = 0
        do while not rs.eof
        %>
subcat[<%=count%>] = new Array("<%= trim(rs("nsort_name"))%>","<%= trim(rs("sort_id"))%>","<%= trim(rs("nsort_id"))%>");
        <%
        count = count + 1
        rs.movenext
        loop
        rs.close
        %>
onecount=<%=count%>;

function changelocation(locationid)
    {
    document.form.nsort_id.length = 0;

    var locationid=locationid;
 document.form.nsort_id.options[0] = new Option('二级分类', '');
    var i;
    for (i=0;i < onecount; i++)
        {
            if (subcat[i][1] == locationid)
            {
                document.form.nsort_id.options[document.form.nsort_id.length] = new Option(subcat[i][0], subcat[i][2]);
            }       
        }
       
    }   
</script>

<%
sql = "select * from nnsort"
rs.open sql,conn,1,1
%>
<script language = "javascript">
var onecount2;
onecount2=0;
subcat2 = new Array();
<%
count2 = 0
do while not rs.eof
%>
subcat2[<%=count2%>] = new Array("<%= trim(rs("nnsort_name"))%>","<%= trim(rs("nsort_id"))%>","<%= trim(rs("nnsort_id"))%>");
<%
count2 = count2 + 1
rs.movenext
loop
rs.close
%>
onecount2=<%=count2%>;

function changelocation2(locationid)
{
document.form.nnsort_id.length = 0;

var locationid=locationid;
var j;
document.form.nnsort_id.options[0] = new Option('三级分类','');
for (j=0;j < onecount2; j++)
{
if (subcat2[j][1] == locationid)
{
document.form.nnsort_id.options[document.form.nnsort_id.length] = new Option(subcat2[j][0], subcat2[j][2]);
}
}

}
</script>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>JS三级连动下拉框</title>
</head>

<body>
<form name="form" method="post" action="">
  <select name="sort_id" onChange="changelocation(document.form.sort_id.options[document.form.sort_id.selectedIndex].value);changelocation2(document.form.nsort_id.options[document.form.nsort_id.selectedIndex].value);">
    <option selected>一级分类</option>
 <%
  sql="select * from sort"
  rs.open sql,conn,1,1
  do while not rs.eof
  %>
  <option value="<%=rs("sort_id")%>"><%=rs("sort_name")%></option>
  <%
    rs.movenext
  loop
  rs.close
  %>
  </select>
  <select name="nsort_id" onChange="changelocation2(document.form.nsort_id.options[document.form.nsort_id.selectedIndex].value);">
    <option selected>二级分类</option>
  </select>
  <select name="nnsort_id">
    <option selected>三级分类</option>
  </select>
</form>
</body>
</html>
<%
set rs=nothing
conn.close
set conn=nothing
%>

转载于:https://www.cnblogs.com/winner/archive/2006/06/04/417240.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值