基于 ASP+AJAX 和数据库驱动的二级联动菜单, 需要的朋友可以参考下。
index.asp 页面代码
<!-#include file = conn.asp->
<%
Establecer cmd = conn.execute (seleccione BigClassid, BigClassName de BigClass)
tempid = cmd (BigClassid)
%>
<Seleccionar nombre = menú OnChange = getSubCategory (this.Value);>
<%
si no cmd.eof entonces
haz mientras que no cmd.eof
BigClassid = CMD (BigClassid)
BigClassName = cmd (BigClassName)
%>
<opción valor = <%= bigclassid%>> <%= bigClassName%> </ppection>
<%
cmd.movenext
bucle
final si
cmd.close
Establecer CMD = nada
%>
</select>
<div ID = subclase>
<Seleccionar nombre = submenu>
<%
establecer cxd = conn.execute (seleccione * de SmallClass donde BigClassid = & Tempid)
si no cxd.eof entonces
haz mientras que no cxd.eof
SmallClassid = CXD (SmallClassid)
SmallClassName = CXD (SmallClassName)%>
<opción valor = <%= smallclassid%>> <%= smallclassname%> </opción>
<%
cxd.movenext
bucle
cxd.lose
establecer cxd = nada
demás
html = <select name = 'smallclassid'> <opción valor = '0' seleccionado> 暂无小类 </opción> </select>
Respuesta.WRITE HTML
final si
%>
</select>
</div>
Ajax.js 代码
// documento JavaScript
función createExmlHttp ()
{
xmlhttpobj = false;
intentar{
xmlhttpobj = new xmlhttprequest;
} catch (e) {
intentar{
xmlhttpobj = new ActiveXObject (msxml2.xmlhttp);
} catch (e2) {
intentar{
xmlhttpobj = new ActiveXObject (microsoft.xmlhttp);
} Catch (E3) {
xmlhttpobj = false;
}
}
}
return xmlhttpobj;
}
función getSubCategory (BigClassid) {
if (bigclassid == 0) {
document.getElementById (subclase) .innerHtml = <select name = 'SmallClassid'> <opción valor = '0' seleccionado> 选择二级分类 </opción> </select>;
devolver;
};
var xmlhttpobj = createExmlhttp ();
if (xmlhttpobj) {// 如果创建对象 xmlhttpobj 成功
xmlhttpobj.onreadyStateChange = handle;
xmlhttpobj.open ('get', getSubCategory.asp? bigclassid =+bigclassid+& number =+math.random (), true); // get 方法 加个随机数。
xmlhttpobj.send (nulo);
}
}
Funcion Handle () {// 客户端监控函数
//if(xmlhttpobj.readystate==4) =// 服务器处理请求完成
if (xmlhttpobj.status == 200) {
// alerta ('OK');
var html = xmlhttpobj.responsetext; // 获得返回值
document.getElementById (subclase) .innerhtml = html;
}demás{
document.getElementById (subclase) .innerhtml = 对不起 , 您请求的页面有问题 ...;
}
//}
//demás{
//document.getElementByid(Subclass).innerhtml=xmlhttpobj.readystate ;// 服务器处理中
//}
//}
}
getSubCategory.asp 代码
<%@idioma = VBScript CodePage = 936%>
<!-#include file = conn.asp->
<%
Respuesta.Charset = GB2312
BigClassid = Safe (request.querystring (BigClassid))
Si BigClassid <> entonces
establecer re = nuevo regexp
re.ignorecase = True
re.global = falso
re.pattern = ^[0-9] {1,3} $
Si no re.test (BigClassid) entonces
Respuesta. Escribe 非法参数
respuesta.
FIN IF%>
<%en el currículum de error siguiente
establecer p = conn.execute (seleccione * de SmallClass donde BigClassid = & BigClassid)
Si err entonces
Err.Clear
Respuesta. Escribe 查询出错
respuesta.
final si
Si no es P.EOF entonces
html = <select name = 'select2'> y vbnewline
hacer mientras no P.EOF
html = html & <option value = '& P (SmallClassid) &'> & P (SmallClassName) & </option> & vbnewline
P.Movenext
bucle
html = html & </select>
demás
html = <select name = 'smallclassid'> <opción valor = '0' seleccionado> 暂无小类 </opción> </select>
final si
p.clar
establecer p = nada
Conn.clar
establecer conn = nada
Respuesta.WRITE HTML
html =
final si
%>