Asp+Ajax二级联动完美实现方法
Asp+Ajax二级联动完美实现方法
<!--#include file="conn.asp教程" -->
<!--
//--------------------------------------------------------
// ASP编程网
// sp+Ajax联动二级联动,转载请保留此信息
-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Asp+Ajax二级联动</title>
<script language="网页特效" type="text/javascript" src="Ajax.js"></script>
</head><body>
<form name="myform" action="MyList.asp" method="post">
<select name="BId" onChange="getoId(this.value,'');">
<option value="">请选择大类</option>
<%
Dim oRs,oSQL
Set oRs = server.CreateObject("adodb.recordset")
oSQL = "Select id,oTitle,oPx From News_Cls Where oId = 0 Order by oPx asc"
oRs.Open oSQL,Conn,1,1
If (oRs.EOF And oRs.BOF) Then
Response.Write("<option value="""">没有添加大类</option>")
Else
Do While Not oRs.EOF
Response.Write("<option value="""&oRs(0)&""">"&oRs(1)&"</option>")
oRs.MoveNext
Loop
End If
oRs.Close:Set oRs = Nothing
%>
</select>
<span id="SmClass"></span>
<input name="" type="submit" value="提交"/>
</form>
</body>
</html>
ajax代码
/*--------------------------------------------------------------------
*
*创建一个XMLHttp实例
*return object 成功创建返回一个XMLHttp对象实例,否则返回false
*
*--------------------------------------------------------------------
*/
function CreateAjax(){
var XMLHttp;
try
{
XMLHttp = new ActiveXObject("Microsoft.XMLHTTP"); //IE的创建方式
}
catch(e)
{
try
{
XMLHttp = new XMLHttpRequest(); //FF等浏览器的创建方式
}
catch(e)
{
XMLHttp = false; //创建失败,返回false
}
}
return XMLHttp; //返回XMLHttp实例
}
//--------------------------------------------------------
// ASP编程网
// http://www.aspprogram.cn/
// Asp+Ajax联动二级联动,转载请保留此信息
function getoId(ids,sId){
var xmlHttp = CreateAjax();
var urlLink = 'List10.asp?oId='+ids+'&sId='+sId+'&n='+Math.random()+'';
if(xmlHttp){
xmlHttp.open('GET',urlLink,true);
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState == 4){ //客户端完成请求
if(xmlHttp.status == 200){ //服务端完成处理并返回数据
var ResponseText = xmlHttp.responseText;
if(ResponseText == ""){ //service返回了错误信息
//alert("yes");
SmClass.innerHTML = ResponseText;
}else{
//alert("no");
SmClass.innerHTML = ResponseText;
}
}else{
SmClass.innerHTML = '服务器返回异常...';//服务器出现异常
}
}else{
SmClass.innerHTML = '正在加载数据...';//请求未完成时的提示信息
}
}
xmlHttp.send(null);//向服务器发送请求
}else{
SmClass.innerHTML = '您的浏览器不支持或未启用 XMLHttp!';//创建未成功
}
}
List10.asp
Response.Charset = "GB2312"
Dim outStr,oRs,oSQL
oId = Request("oId")
sId = Request("sId")
If (oId = "" Or IsNull(oId)) Then
outStr = ""
Else
outStr = "<select name=""SmId"">"
outStr = outStr &"<option value=""0"">不指定小类</option>"
oSQL = "Select id,oTitle,oPx,oId From News_Cls Where oId = "&oId&" "
oSQL = oSQL &" Order by oPx asc"
Set oRs = Conn.Execute(oSQL)
If (oRs.EOF And oRs.BOF) Then
outStr = outStr &"<option value=""0"">暂无小类</option>"
Else
Do While Not oRs.EOF
If Trim(sId) = Trim(oRs(0)) Then
outStr = outStr &"<option value="""&oRs(0)&""" selected>"&oRs(1)&"</option>"
Else
outStr = outStr &"<option value="""&oRs(0)&""">"&oRs(1)&"</option>"
End If
oRs.MoveNext
Loop
End If
outStr = outStr &"</select>"
Set oRs = Nothing
End If
Response.Write(outStr)
conn.asp
Dim Mdb,Conn,ConnStr
Mdb="DataBase/AdSl.mdb"
Set Conn = Server.CreateObject("ADODB.Connection")
ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath(Mdb) & " "
Conn.open ConnStr
Sub CloseConn()
If IsObject(Conn) Then
Conn.Close
Set Conn = Nothing
End If
End Sub
补充:asp教程,高级应用