ASP+JS三级连动下拉框


<%
'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
%>

posted @ 2006-06-04 22:39  Winner.Net(2007)  阅读(1470)  评论(0编辑  收藏  举报