基于asp+ajax和数据库驱动的二级联动菜单、需要的朋友可以参考下。
index.asp页面代码
<! - #include file = conn.asp->
<%
cmd = conn.executeを設定します(bigclassid、bigclassnameをbigclassから選択します)
tempid = cmd(bigclassid)
%>
<select name = menu onchange = getsubcategory(this.value);>
<%
cmd.eofではない場合
cmd.eofではありません
bigclassid = cmd(bigclassid)
bigclassname = cmd(bigclassname)
%>
<オプション値= <%= bigclassid%>> <%= bigclassname%> </option>
<%
cmd.movenext
ループ
ifを終了します
cmd.close
CMD =何も設定しません
%>
</select>
<div id = subclass>
<選択= submenu>を選択します
<%
cxd = conn.execute(select * from smallclass bigclassid =&tempid)を設定します
cxd.eofではない場合
cxd.eofではありません
smallclassid = cxd(smallclassid)
SmallClassName = CXD(SmallClassName)%>
<オプション値= <%= SmallClassID%>> <%= SmallClassName%> </option>
<%
cxd.movenext
ループ
cxd.close
cxd =何も設定しません
それ以外
html = <select name = 'smallclassid'> <option value = '0' selected> </option> </select>
Response.Write HTML
ifを終了します
%>
</select>
</div>
ajax.js代码
// JavaScriptドキュメント
関数createxmlhttp()
{
xmlhttpobj = false;
試す{
xmlhttpobj = new xmlhttprequest;
} catch(e){
試す{
xmlhttpobj = new ActiveXObject(msxml2.xmlhttp);
} catch(e2){
試す{
xmlhttpobj = new ActiveXObject(microsoft.xmlhttp);
} catch(e3){
xmlhttpobj = false;
}
}
}
xmlhttpobjを返します。
}
function getsubcategory(bigclassid){
if(bigclassid == 0){
document.getElementById(subclass).innerhtml = <select name = 'smallclassid'> <option value = '0' selected>选择二级分类</option> </select>;
戻る;
};
var xmlhttpobj = createxmlhttp();
if(xmlhttpobj){//
xmlhttpobj.onreadystatechange = handle;
xmlhttpobj.open( 'get'、getsubcategory.asp?bigclassid =+bigclassid+&number =+math.random()、true); //
xmlhttpobj.send(null);
}
}
function handle(){//客户端监控函数
//if(xmlhttpobj.readystate==4) {//服务器处理请求完成
if(xmlhttpobj.status == 200){
// alert( 'ok');
var html = xmlhttpobj.responsetext; //获得返回值
document.getElementByID(subclass).innerhtml = html;
}それ以外{
document.getElementById(subclass).innerhtml =对不起、您请求的页面有问题...;
}
//}
//それ以外{
//document.getElementById(subclass).innerhtml=xmlhttpobj.ReadyState;///服务器处理中
//}
//}
}
getSubcategory.asp代码
<%@言語= vbscript codepage = 936%>
<! - #include file = conn.asp->
<%
Response.Charset = GB2312
bigclassid = safe(request.querystring(bigclassid))
bigclassid <>の場合
re = new regexpを設定します
re.ignorecase = true
re.global = false
re.pattern = ^[0-9] {1,3} $
re.test(bigclassid)ではない場合
Response.Write非法参数
Response.End
if>を終了します
次にエラー再開時の<%
set p = conn.execute(select * from smallclass bigclassid =&bigclassid)
エラーの場合
err.clear
Response.Write查询出错
Response.End
ifを終了します
そうでない場合は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> </select>
ifを終了します
P.Close
p =何も設定しません
conn.close
conn =何も設定しません
Response.Write HTML
html =
ifを終了します
%>