基于 ASP+Ajax 和数据库驱动的二级联动菜单, 需要的朋友可以参考下。
index.asp 页面代码
<!-#include File = conn.asp->
<%
Setzen Sie CMD = Conn.Execute (SELECT BIGCASSID, BigClassName von BigClass)
tempid = cmd (bigclassid)
%>
<SELECT NAME = MENUS OnChange = GetUbCategory (this.Value);>
<%
Wenn nicht cmd.eof dann
tun, während nicht cmd.eof
BigClassid = CMD (Bigclassid)
BigClassName = CMD (BigClassName)
%>
<Option Value = <%= BigClassid%>> <%= BigClassName%> </Option>
<%
Cmd.Movenext
Schleife
Ende wenn
cmd.close
Setzen Sie CMD = nichts
%>
</select>
<div id = subclass>
<SELECT NAME = SUBMENU>
<%
Setzen Sie CXD = conn.execute (SELECT * aus SmallClass, wo BigClassid = & tempid)
Wenn nicht cxd.eof dann
tun, während nicht cxd.eof
smallclassid = cxd (smallclassid)
smallclassName = cxd (smallClassName)%>
<Optionswert = <%= smallclassid%>> <%= smallClassName%> </Option>
<%
CXD.Movenext
Schleife
CXD.CLOSE
Setzen Sie CXD = nichts
anders
html = <select name = 'smallclassid'> <option value = '0' ausgewählt> 暂无小类 </option> </select>
response.write html
Ende wenn
%>
</select>
</div>
ajax.js 代码
// JavaScript -Dokument
Funktion createxmlhttp ()
{
xmlhttpobj = false;
versuchen{
xmlhttpobj = neu xmlhttprequest;
} catch (e) {
versuchen{
xmlhttpobj = new ActiveXObject (msxml2.xmlhttp);
} catch (e2) {
versuchen{
xmlhttpobj = new ActiveXObject (microsoft.xmlhttp);
} catch (e3) {
xmlhttpobj = false;
}
}
}
return xmlhttpobj;
}
Funktion getuBcategory (Bigclassid) {
if (bigclassid == 0) {
document.getElementById (Unterklasse) .innerhtml = <select name = 'smallClassid'> <option value = '0' ausgewählt> 选择二级分类 </option> </select>;
zurückkehren;
};
var xmlhttpobj = createxmlhttp ();
if (xmlhttpobj) {// 如果创建对象 xmlhttpobj 成功
xmlhttpobj.onReadyStatechange = Handle;
xmlhttpoBj.open ('get', getUbUbCategory.asp? BigClassid =+BigClassid+& number =+math.random (), true); // GET 方法 加个随机数。 加个随机数。 加个随机数。 加个随机数。 加个随机数。 加个随机数。 加个随机数。 加个随机数。 加个随机数。 加个随机数。
xmlhttpobj.send (null);
}
}
Funktion Handle () {// 客户端监控函数
//if(xmlhttpobj.readystate==4) <// 服务器处理请求完成
if (xmlhttpobj.status == 200) {
// alert ('ok');
var html = xmlhttpobj.responsetext; // 获得返回值
document.getElementById (Unterklasse) .innerhtml = html;
}anders{
document.GetElementById (Unterklasse) .innerhtml = 对不起 , 您请求的页面有问题 ...;
}
//}
//anders{
//document.getElementById(SubClass).innerhtml=xmlHttpobj.readystate;// 服务器处理中
//}
//}
}
getUbcategory.asp 代码
<%@Sprache = vbscript codepage = 936%>
<!-#include File = conn.asp->
<%
Antwort.CharSet = GB2312
BigClassid = Safe (Anfrage.queryString (Bigclassid))
Wenn Bigclassid <> dann
SET RE = New Regexp
re.Innorecase = true
re.global = false
re.Pattern = ^[0-9] {1,3} $
wenn nicht re.test (bigclassid) dann
Antwort.Write 非法参数
Antwort.end
Ende wenn%>
<%beim nächsten Fehlerlebenslauf als nächstes
Setzen Sie p = conn.execute (select * aus smallClass, wo Bigclassid = & bigclassid)
Wenn irren
err.clear
Antwort.Write 查询出错
Antwort.end
Ende wenn
Wenn nicht P.Eof dann
html = <select name = 'select2'> & vbNewline
tun, während nicht P.Eof
html = html & <option value = '& p (smallclassid) &'> & p (smallclassName) & </option> & vbNewline
p.Movenext
Schleife
html = html & </select>
anders
html = <select name = 'smallclassid'> <option value = '0' ausgewählt> 暂无小类 </option> </select>
Ende wenn
P.CLOSE
Setzen Sie P = nichts
conn.close
Setzen Sie Conn = nichts
response.write html
html =
Ende wenn
%>