当前位置: 首页 > 科教园地 > 技术资料
基于ajax的二级联动的一个例子
时间:2011-07-25

基于ajax的二级联动的例子。

<!--#include file="include/setting.asp" -->
<!--#include file="include/Conn.asp" -->

 <% 
 function listxb()
 
    dim sql
    dim rs
 sql="select * from xb "
 set rs=server.CreateObject("adodb.recordset")
 rs.open sql,conn,3,3
 while not rs.eof
      response.Write("<option value=" & rs("id") & ">" & rs("dname") & "</option>")
      rs.movenext
 wend
end function
%>
<head>
<style>
#zhy { width:200px;}
</style>
<script type="text/javascript" language="javascript">
var xmlHttp;
function createXMLHttpRequest() { //创建XMLHttp实例,考虑到兼容性问题,要做判断
    if (window.ActiveXObject) { //IE6以前
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if (window.XMLHttpRequest) { //其他浏览器
        xmlHttp = new XMLHttpRequest();
    }
}
 
function startRequest() {
    createXMLHttpRequest();
    xmlHttp.onreadystatechange = handleStateChange;  //异步调用的回调函数
 var currSelectValue = document.all.xb.value;
    xmlHttp.open("GET", "test3.asp?id="+ currSelectValue , true); //连接服务器并访问
    xmlHttp.send(null);
}
 
function handleStateChange() {
 if(xmlHttp.readyState == 4) { //如果readyState==4 已经收到所有数据,可使用所有数据
        if(xmlHttp.status == 200) { //status==200,表示正常,结果在xmlHttp.responseText中
    var s;
    s=document.getElementById("result");
    s.innerHTML=unescape(xmlHttp.responseText); //利用javascript +dom 显示返回结果
        }
    }
}
 
</script>
<script language="javascript" runat="server">
function encode(str){
   return escape(str);
}
function decode(str){
   return unescape(str);
}
</script>
</head>
<body onload="javascript:startRequest()">
<table cellpadding="0" cellspacing="0" border="0" width="500" align="center">
<tr>
<td align="center" width="300" >
    <select id="xb" name="xb"size="1"  onchange="javascript:startRequest();">
  <% call listxb() %>
 </select>
</td>
<td>
   <div id="result">
</div>

</td>
</tr>
</table>
</body>
</html>
 
<!--#include file="include/setting.asp" -->
<!--#include file="include/Conn.asp" -->
<%@language="vbscript" codepage="936" %>
 <% 
    Function VbsEscape(str)
        dim i,s,c,a
        s=""
        For i=1 to Len(str)
            c=Mid(str,i,1)
            a=ASCW(c)
            If (a>=48 and a<=57) or (a>=65 and a<=90) or (a>=97 and a<=122) Then
                s = s & c
            ElseIf InStr("@*_+-./",c)>0 Then
                s = s & c
            ElseIf a>0 and a<16 Then
                s = s & "%0" & Hex(a)
            ElseIf a>=16 and a<256 Then
                s = s & "%" & Hex(a)
            Else
                s = s & "%u" & Hex(a)
            End If
        Next
        VbsEscape=s
    End Function
    '与javascript中的unescape()等效
    Function VbsUnEscape(str)
                    Dim x
        x=InStr(str,"%")
        Do While x>0
            VbsUnEscape=VbsUnEscape&Mid(str,1,x-1)
            If LCase(Mid(str,x+1,1))="u" Then
                VbsUnEscape=VbsUnEscape&ChrW(CLng("&H"&Mid(str,x+2,4)))
                str=Mid(str,x+6)
            Else
                VbsUnEscape=VbsUnEscape&Chr(CLng("&H"&Mid(str,x+1,2)))
                str=Mid(str,x+3)
            End If
            x=InStr(str,"%")
        Loop
        VbsUnEscape=VbsUnEscape&str
    End Function

     'response.Charset="gb2312"
    dim sql ,r
    dim rs
 sql="select * from zhy where did=" & request.QueryString("id")
 'sql="select * from zhy"
 set rs=server.CreateObject("adodb.recordset")
 rs.open sql,conn,3,3
 r=" <select id=zhy name=zhy size=1 > "
 while not rs.eof
      r=r & " <option value='" & rs("id") & "'> " & rs("cname") & "</option> "
   r=r & rs("CName") &  " "
      rs.movenext
 wend
 r=r & "</select> "
 'response.Charset="gb2312"
    response.Write(VbsEscape(r))
 rs.close
 set rs=nothing
%>