我想用jsp和javascript作两个联动下拉框(<select>):
有表1。2。3
表1  indexdb
 id    dbname    cname
 1     userdb    用户表
 2     deptdb    部门表 表2  userdb
 id    username    paswd
 1     aaa         aaa
 2     bbb         bbb表3  deptdb
 id    deptname    cname
 1     rs         人事
 2     cw         财务第一个下拉框的option显示表indexdb的cname内容
第二个下拉框的option则随着第一个下拉框选择变化相应的显示相应表的字段名
第一个下拉框好实现,而我遇到的问题是不知道怎样写javascript的onchange的函数把第二个下拉框的内容显示出来,
请大侠来帮帮忙吧!!!

解决方案 »

  1.   

    <%
    dim rs
    set rs=conn.execute("select * from nclass order by id asc")
    %>
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <script>
    var onecount;
    onecount=0;
    subcat = new Array();
    <%
    dim count
    count=0
    do while not rs.eof 
    %>
    subcat[<%=count%>] = new Array("<%= trim(rs("nclassname"))%>","<%= trim(rs("classid"))%>","<%= trim(rs("id"))%>");
    <%
    count = count + 1
    rs.movenext
    loop
    rs.close
    %>
    onecount=<%=count%>;function changelocation(locationid)
        {
        document.form1.nclassid.length = 0;     var locationid=locationid;
        var i;
        for (i=0;i < onecount; i++)
            {
                if (subcat[i][1] == locationid)
                { 
                    document.form1.nclassid.options[document.form1.nclassid.length] = new Option(subcat[i][0], subcat[i][2]);
                }        
            }
            
        } function report()
    {
    var options_string = "";
    var the_select = window.document.form1.nclassid;
    for (loop=0; loop < the_select.options.length; loop++)
    {
    if (the_select.options[loop].selected == true)
    { options_string += the_select.options[loop].text;
    }
    }
    window.document.form1.nclassn2.value=options_string;
    }</script>
    <link href="../Images/style.css" rel="stylesheet" type="text/css">
    <style type="text/css">
    <!--
    .style1 {color: #FFFFFF}
    -->
    </style>
    </head><body>
    <form name="form1" method="post" action="">
      <table width="100%" border="0" cellspacing="0" cellpadding="0" align="center" bgcolor="#FFFFFF">
        <tr>
          <td width="100%">
            <div align="center"></div></td>
        </tr>
        <tr>
          <td width="100%">
            <table width="100%" border="1" cellspacing="0" cellpadding="2" bordercolordark="#ffffff" bordercolorlight="#000000">
              <tr>
                <td height="25" bgcolor="#23458B"><span class="style1">添加二级类别:</span></td>
              </tr>
              <tr>
                <td height="25"><%
    sql="select * from class order by id asc"
    set rs=conn.execute(sql)
    if not rs.eof then
    %>
                选择类别:
                  <select name="classid" id="select" onChange="report();">
                    <option value="">--请选择类别--</option>
                    <%do while not rs.eof%>
                    <option value="<%=rs("id")%>"><%=rs("classname")%></option>
                    <%rs.movenext
                  loop
                %>
                  </select>
                填写二级类别名称:
                <input name="nclassname" type="text" id="nclassname2" size="15">
                <input type="submit" name="Submit" value="添加">
                <%else%>
                没有任何类别,请添加类别!
                <%end if%></td>
              </tr>
            </table>
            <br/>
            <table width="100%" border="1" cellspacing="0" cellpadding="2" bordercolordark="#ffffff" bordercolorlight="#000000">
              <tr>
                <td height="25" bgcolor="#23458B"><span class="style1">对类别进行添加和删除:</span></td>
              </tr>
              <tr>
                <td height="50">
                  <%
      dim selclass
    sql="select * from class"
    set rs=conn.execute(sql)
    if not rs.eof then
    %>
                选择类别:
                <select name="classid2" id="classid2" onChange="changelocation(document.form1.classid2.options[document.form1.classid2.selectedIndex].value)">
                  <option value="">--请选择类别--</option>
      <%
      if not rs.eof then
    selclass=rs("id")
      %>
       <option value="<%=rs("id")%>" selected><%=rs("classname")%></option>
       <%
       rs.movenext
       end if%>
                  <%do while not rs.eof%>
                  <option value="<%=rs("id")%>"><%=rs("classname")%></option>
                  <%
      rs.movenext
                  loop
                %>
                </select>
                <select name="nclassid" onChange="report();">
    <%
    sql="select * from nclass where classid='"&selclass&"' order by id asc"
    set rs=conn.execute(sql)
    if not(rs.eof and rs.bof) thendo while not rs.eof%>
                  <option value="<%=rs("id")%>"><%=rs("nclassname")%></option>
                  <% rs.movenext
    loop
    end if
    %>
                </select>
                <br/>
                类别名称:
                <input name="nclassn2" type="text" id="nclassn2">
                <input type="submit" name="Submit" value="修改">
                <input type="button" name="Submit" value="删除" onClick="nclass_del();">
                <%else%>
                没有任何类别,请添加类别!
                <%end if%>
                </td>
              </tr>
          </table></td>
        </tr>
      </table>
    </form>
    </body>
    </html>
      

  2.   

    这样的东西最好用xml或者ajax技术来实现,这是比较正统的做法
      

  3.   

    以前用asp做过,不过我想差不多,思路都一样.
    在第一个下拉框的onchange事件里提交表单给自己,然后在页面刚开始接收提交到的第一个下拉框信息,根据这个提交信息去数据库里读出相应数据,再把这些数据用循环动态的添加到第二个下拉框中.思路就是这样,楼主可以自己根据思路想想代码怎么写
      

  4.   

    lip009(深蓝忧郁) :
    你说的在第一个下拉框的onchange事件里提交表单给自己,具体怎么作啊
    我初学,只作过用submit按钮提交表单啊!!
      

  5.   

    wsk_228(qing_feng) 的联动和我的这个好像不太一样啊
    还有哪位能给指点指点啊
    我需要在javascript函数中传表名值怎么来实现啊!!!
      

  6.   

    不好意思,今天刚看到你的问题,你的问题很简单哦,就是设置form表单的action=""就可以了