首页
印 像 新 闻 系 统
网站首页 公告信息 编程实例 PHP函数 摄影无界 全景展示 我的作品 意见专栏
 

ASP+JS实现二级分类下拉列表联动

【2006年03月11日 21:59:42】 【阅读:次】 【字体:大 中 小 默认】 【背景:#换背景 #换背景 #换背景 #换背景 #换背景 #换背景 #换背景 默认】

 

把数据读到JS中

<%
dim count
set rsn=server.createobject("adodb.recordset")
rsn.open "select * from [articlesbid] order by [row]",conn,1,1
%>
<script language = "JavaScript">
var onecount;
onecount=0;
subcat = new Array();
<%
   count = 0
   do while not rsn.eof
%>
subcat[<%=count%>] = new Array("<%= trim(rsn("Nclass"))%>","<%= rsn("classid")%>","<%= rsn("Nclassid")%>");
<%
       count = count + 1
       rsn.movenext
   loop
   rsn.close
%>
onecount=<%=count%>;

function changelocation(locationid)
    {
    document.myform.Nclassid.length = 0;

    var locationid=locationid;
    var i;
    for (i=0;i < onecount; i++)
        {
            if (subcat[i][1] == locationid)
            {              document.myform.Nclassid.options[document.myform.Nclassid.length] = new Option(subcat[i][0], subcat[i][2]);
            }       
        }
    }   

一级栏目:

<%
        rsn.open "select * from [articlesaid] order by [row]",conn,1,1
     if rsn.eof and rsn.bof then
        response.write "请先添加栏目。"
        response.end
     else%>
  <select name="anclassid" size="1" id="anclassid" onChange="changelocation(document.myform.anclassid.options[document.myform.anclassid.selectedIndex].value)">
          <option selected value="<%=rsn("classid")%>"><%=trim(rsn("class"))%></option>
          <%dim selclass
            selclass=rsn("classid")
            rsn.movenext
            do while not rsn.eof%>
          <option value="<%=rsn("classid")%>"><%=trim(rsn("class"))%></option>
          <%
              rsn.movenext
            loop
     end if
        rsn.close%>
        </select>

二级栏目:
  

<select name="Nclassid">
          <%rsn.open "select * from [articlesbid] where [classid]="&selclass ,conn,1,1
        if not(rsn.eof and rsn.bof) then%>
          <option selected value="<%=rsn("Nclassid")%>"><%=trim(rsn("Nclass"))%></option>
          <%rsn.movenext
          do while not rsn.eof%>
          <option value="<%=rsn("Nclassid")%>"><%=trim(rsn("Nclass"))%></option>
          <% rsn.movenext
          loop
         end if
         rsn.close
         set rsn = nothing%>
        </select>

$Page_Split$

(补充)更改栏目时将数据库中栏目设为默认值:

<%dim rs1
    set rs1=server.CreateObject("adodb.recordset")
    rs1.open "select * from [articles] where id="&clng(cid),conn,1,1
    rsn.open "select * from [articlesaid] order by row",conn,1,1
 if rsn.eof and rsn.bof then
 response.write "请先添加栏目。"
 response.end
 else
    %>
    <select name="ANclassid"  size="1" id="anclassid" onChange="changelocation(document.myform.anclassid.options[document.myform.anclassid.selectedIndex].value)">
    <%do while not rsn.eof%>
         <option value="<%=rsn("classid")%>" <%if rs1("classid")=rsn("classid") then%>selected<%end if%>><%=trim(rsn("class"))%></option>
         <%rsn.movenext
      loop
 end if
    rsn.close%>
    </select>
    <select name="Nclassid">
    <%rsn.open "select * from [articlesbid] where classid="&rs1("classid") ,conn,1,1
    do while not rsn.eof%>
       <option value="<%=rsn("Nclassid")%>" <%if rs1("Nclassid")=rsn("Nclassid") then%>selected<%end if%>><%=rsn("Nclass")%></option>
       <%rsn.movenext
    loop
    rsn.close
    set rsn= nothing%>
    </select>


(『印像空间』 【本文来源:『笔记』】 【作者:未知】 【录入:留印】)
限200字,剩余 字
关 键 字:
    
搜索条件: