解决双下拉选单的连动问题
就以选取省份后改变城市选项为例,省份、城市数据存放于后台MSSQL Server,就一个表:Citys:字段State、City。程序:
CitySelect.asp
<%@ LANGUAGE="vbscript" %>
<%
strPathInfo = Request.ServerVariables("PATH_INFO")
strPathInfo = "http://" & Request.ServerVariables("SERVER_NAME") & Left(strPathInfo,InstrRev(strPathInfo,"/"))
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Provider = "sqloledb"
Conn.Open "Data Source=ServerName;Initial Catalog=dbName;", "sa", ""
Set RS = Conn.Execute("SELECT DISTINCT state FROM citys")
arrPr = RS.GetRows()
RS.Close
Set RS = Nothing
Conn.Close
Set Conn = Nothing
%>
<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=gb_2312-80">
<TITLE></TITLE>
<SCRIPT language=javascript>
<!--
function ChooseCity()
{
file://Clear City List
for(var i=form1.SelCity.options.length-1;i>=0;--i)
{
form1.SelCity.options.remove(i)
}
var sState = form1.SelState.options(form1.SelState.selectedIndex).value;
var oXMLDoc = new ActiveXObject(@#MSXML@#);
sURL = @#<%=strPathInfo%>XMLCity.asp?State=@# + sState;
oXMLDoc.url = sURL;
var oRoot=oXMLDoc.root;
if(oRoot.children != null)
{
for(var i=0;i<oRoot.children.length;++i)
{
oItem = oRoot.children.item(i);
sName = oItem.text;
var oOption = document.createElement(@#OPTION@#);
oOption.text = sName;
oOption.value = sName;
form1.SelCity.options.add(oOption);
}
}
}
-->
</SCRIPT>
</HEAD>
<BODY>
<FORM action="" method="post" id="form1" name="form1">
<SELECT name="SelState" id="SelState" οnchange="ChooseCity()">
<%For i = LBound(arrPr,2) To UBound(arrPr,2)%>
<OPTION value="<%=arrPr(0,i)%>"><%=arrPr(0,i)%></OPTION>
<%Next%>
</SELECT>
<SELECT name="SelCity" id="SelCity">
</SELECT>
</FORM>
</BODY>
</HTML>
--------------------------
XMLCity.asp
<%@ LANGUAGE="VBSCRIPT" %>
<%
sState = Request.QueryString("State")
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Provider = "sqloledb"
Conn.Open "Data Source=ServerName;Initial Catalog=dbName;", "sa", ""
Set RS = Conn.Execute("SELECT city FROM citys WHERE state=@#" & sState & "@#")
arrCity = Rs.GetRows()
%>
<?xml version="1.0" encoding="gb2312" ?>
<citys>
<%For i = LBound(arrCity,2) To UBound(arrCity,2)%>
<city><%=arrCity(0,i)%></city>
<%Next%>
</citys>
--------------------------
几点说明:
1、简单的说,这种方法就是利用客户端MSXMl对象去load一个xml,而这个xml文件的后缀是asp,呵呵,传个querysting过去就可以为所欲为了,好处是显然的:页面不刷新;传输的数据少,不需要把所有数据全传到客户端 :)
2、这种方法Zee最早是在White前辈的一篇帖子里看到的,不过他老人家的sample是用来取服务器端的时间,大材小用阿。 :P
3、本方法对ie4以上版本有效。(这也是为什么不用"microsoft.XMLDOM"对象而用"msxml"的原因)