基于asp+ajax和数据库驱动的二级联动菜单,需要的朋友可以参考下。
index.asp页面代码
<! - #include file = conn.asp->
<%
设置cmd = conn.execute(选择bigclassid,bigclassname来自bigclass)
tempid = cmd(bigclassid)
%>
<select name =菜单onChange = getSubCategory(this.value);>
<%
如果不是cmd.eof,则
在不cmd.eof时做
BigClassid = CMD(BigClassid)
BigClassName = CMD(BigClassName)
%>
<option value = <%= bigClassid%>> <%= bigClassName%> </option>
<%
cmd.movenext
环形
如果结束
cmd.close
设置CMD =没有
%>
</select>
<div id = subclass>
<select name =子菜单>
<%
设置cxd = conn.execute(从bigclassid =&tempid中选择 * *
如果不是cxd.eof,则
在不cxd.eof时做
SmallClassid = CXD(SmallClassid)
SmallClassName = CXD(SmallClassName)%>
<option value = <%= smallClassid%>> <%= SmallClassName%> </option>
<%
cxd.movenext
环形
cxd.close
设置CXD =没有
别的
html = <select name ='smallclassid'> <option value ='0 selected>暂无小类</option> </oplect>
wress.write html
如果结束
%>
</select>
</div>
ajax.js代码
// JavaScript文档
函数createxmlhttp()
{
xmlhttpobj = false;
尝试{
xmlhttpobj = new xmlhttprequest;
}捕获(e){
尝试{
xmlhttpobj = new ActiveXobject(msxml2.xmlhttp);
} catch(e2){
尝试{
xmlhttpobj = new ActiveXobject(Microsoft.xmlhttp);
} catch(e3){
xmlhttpobj = false;
}
}
}
返回XMLHTTPOBJ;
}
函数getSubCategory(bigClassid){
如果(bigClassid == 0){
document.getElementById(subclass).innerhtml = <select name ='smallClassId'> <option value ='0 selected>选择二级分类</option> </option> </select>;
返回;
};
var xmlhttpobj = createxmlhttp();
if(xmlhttpobj){//如果创建对象xmlhttpobj成功
xmlhttpobj.onreadystatechange = handle;
xmlhttpobj.open('get',get ubcategory.asp?bigClassid =+bigClassid+&number =+Math.random(),true); // get方法
xmlhttpobj.send(null);
}
}
函数hander(){//客户端监控函数
//if(XMLHTTPOBJ.READYSTATE = 4) {///
if(xmlhttpobj.status == 200){
// alert('ok');
var html = xmlhttpobj.responsetext; //获得返回值
document.getElementById(subclass).innerhtml = html;
}别的{
document.getElementById(子类).innerhtml =对不起,您请求的页面有问题...;
}
//}
//别的{
//document.getElementById(subclass).innerhtml = xmlhttpobj.dreadystate ;///服务器处理中
//}
//}
}
getSubCategory.asp代码
<%@语言= vbscript codepage = 936%>
<! - #include file = conn.asp->
<%
响应。charset= gb2312
bigClassid = safe(request.querystring(bigClassid))
如果BigClassid <>
设置RE =新的Regexp
re.ignorecase = true
re.global = false
re.pattern = ^[0-9] {1,3} $
如果不是re.test(bigclassid),则
响应
响应
如果%>结束
<%错误简历下一步
设置p = conn.execute(从bigClassid =&bigClassid中选择 * *
如果err,然后
err.CLEAR
响应
响应
如果结束
如果不是p.eof,则
html = <select name ='select2'>&vbnewline
虽然不p.eof
html = html&<option value ='&p(smallclassid)&'>&p(smallclassname)&</option>&vbnewline
P.Movenext
环形
html = html&</select>
别的
html = <select name ='smallclassid'> <option value ='0 selected>暂无小类</option> </oplect>
如果结束
p.close
设置p =没有
conn.close
设置conn =没有
wress.write html
html =
如果结束
%>