基于 Asp + ajax 和数据库驱动的二级联动菜单, 需要的朋友可以参考下。
index.asp 页面代码
<! - # Inclure File = Conn.asp ->
<%
Définissez CMD = Conn.Execute (sélectionnez BigClassid, BigclassName de BigClass)
tempid = cmd (bigclassid)
%>
<select name = menu onchange = getSubCategory (this.value);>
<%
Si ce n'est pas cmd.eof alors
faire tout en ne faisant pas cmd.eof
bigclassid = cmd (bigclassid)
bigclassname = cmd (bigclassname)
%>
<Valeur d'option = <% = bigClassid% >> <% = bigclassname%> </ option>
<%
cmd.movènext
boucle
terminer si
cmd.close
Définir CMD = rien
%>
</lect>
<div id = sous-classe>
<select name = subMenu>
<%
Définissez CXD = Conn.Execute (SELECT * FROM SMALTCLASS WHERE BIGCLASSID = & Tempid)
Si pas cxd.eof alors
faire tout en ne faisant pas cxd.eof
smallclassid = cxd (smallclassid)
smallclassname = cxd (smallclassname)%>
<Valeur de l'option = <% = smallclassid% >> <% = smallclassname%> </ option>
<%
cxd.movènext
boucle
cxd.close
définir cxd = rien
autre
html = <select name = 'smallclassid'> <option value = '0' sélectionné> 暂无小类 </ option> </lect>
réponse.WRITE HTML
terminer si
%>
</lect>
</div>
ajax.js 代码
// Document JavaScript
fonction createExmlhttp ()
{
xmlhttpobj = false;
essayer{
xmlHttpObj = new xmlHttpRequest;
} catch (e) {
essayer{
xmlHttpObj = new activeXObject (msxml2.xmlhttp);
} catch (e2) {
essayer{
xmlHttpObj = new activeXObject (Microsoft.xmlhttp);
} catch (e3) {
xmlhttpobj = false;
}
}
}
return xmlHttpObj;
}
Fonction getSubCategory (bigClassid) {
if (bigcllassid == 0) {
document.getElementById (sous-classe) .InnerHtml = <Select Name = 'smallClasSID'> <Option Value = '0' sélectionné> 选择二级分类 </opoption> </lect>;
retour;
};
var xmlhttpobj = createExmlHttp ();
if (xmlHttpObj) {// 如果创建对象 xmlhttpobj 成功
xmlhttpobj.onreadystateChange = handle;
xmlhttpobj.open ('get', getSubCategory.asp? bigclassid = + bigClassid + & nombre = + math.random (), true); // get 方法 加个随机数。
xmlhttpobj.send (null);
}
}
Handle de fonction () {// 客户端监控函数
//if(xmlhttpobj.readystate==4) {// 服务器处理请求完成
if (xmlhttpobj.status == 200) {
// alert ('ok');
var html = xmlhttpobj.ResponSeText; // 获得返回值
document.getElementById (sous-classe) .InnerHtml = html;
}autre{
document.getElementById (sous-classe) .InnerHtml = 对不起 , 您请求的页面有问题 ...;
}
//}
//autre{
//Document.getElementById(SubClass).innerHtml=xmlhttpobj.readystate ;// 服务器处理中
//}
//}
}
getSubCategory.asp 代码
<% @ language = codepage VBScript = 936%>
<! - # Inclure File = Conn.asp ->
<%
réponse.Charset = GB2312
BigClassid = Safe (request.Querystring (bigclassid))
Si bigclassid <> alors
Définir Re = nouveau regexp
re.ignorecase = true
re.global = false
re.Pattern = ^ [0-9] {1,3} $
Si pas re.test (bigclassid) alors
réponse.WRITE 非法参数
réponse.
terminer si%>
<% sur l'erreur CV Suivant
Définissez P = Conn.ExECUTE (SELECT * FROM SMALTCLASS WHERE BIGCLASSID = & BIGCLASSID)
Si err alors
err.Clear
réponse.WRITE 查询出错
réponse.
terminer si
Sinon P.Eof alors
html = <select name = 'select2'> & vbnewline
faire tout en ne faisant pas
html = html & <option value = '& p (smallclassid) &'> & p (smallclassname) & </ option> & vbnewline
P.MovEnext
boucle
html = html & </lect>
autre
html = <select name = 'smallclassid'> <option value = '0' sélectionné> 暂无小类 </ option> </lect>
terminer si
P.Close
Définir P = rien
Conn.Close
Définir Conn = Rien
réponse.WRITE HTML
html =
terminer si
%>