从数据库中动态选取下拉列表的方法

网络整理 - 09-11
有一页填表的页面,其中有几个下拉表单是通过一个表的某个字段动态生成,比如省下面的城市名,希望选择某一个
省即可自动在另一下拉表单内生成城市名,.....然后提交给asp处理加入到另外一个表内。

  终于完成了,代码如下,可能比较乱,不过还是很有效的,不用刷新页面。
大家不妨一试~~~~~~~~

<form name=f1 METHOD="POST">
<%
OpenDB objConn, "xxxx"
Set RSClass = objConn.Execute("SELECT * FROM class Order by fldClass")
If RSClass.EOF Then    
Response.Write "没有记录。<BR>"

Else

      Response.Write "<SELECTclass""FONT-SIZE: 9pt"" SIZE=10" & _
        " ONCHANGE=""classselected(this);"" >"
        sJavascript = "function classselected(elem){" &_
         vbCrlf & _
        "for (var i = document.f1.sort.options.length; i >= 0; i--){" & vbCrlf & _
        "document.f1.sort.options[i] = null;" & _
         vbCrlf
   Do Until RSClass.EOF
     If sLastClass <> RSClass("fldClass") Then
         sLastClass = RSClass("fldClass")
        Response.Write "<OPTION VALUE=" & RSClass("fldID") & ">" & sLastClass & "</OPTION>"
         sJavascript = sJavascript & "}" & vbCrlf & _
                "if (elem.options[elem.selectedIndex].value==" & _
                 RSClass("fldID") & "){" & vbCrlf
      End If
         sJavascript = sJavascript & _
                 "document.f1.sort.options[document." & _
                 "f1.sort.options.length] = new Option('" & _
                  RSClass("fldSort") & "','" & RSClass("fldID") & "');" & _
                  vbCrlf
     RSClass.MoveNext
     Loop
        Response.Write "</SELECT>"
        Response.Write "<SELECTsort""FONT-SIZE: 9pt"" SIZE=10>"
        Response.Write "<OPTION>[请选择]</OPTION>"
        Response.Write "</SELECT>"
    
        sJavascript = sJavascript & vbCrlf & "}" & vbCrlf & "}" & vbCrlf
        Response.Write "<SCR" & "IPT LANGUAGE=""Javascript"">" & vbCrlf
        Response.Write sJavascript & vbCrlf & "</SCR" & "IPT>" & vbCrlf
End If
RSClass.Close
Set RSClass = Nothing%>
</form>