基于 Asp+Ajax 和数据库驱动的二级联动菜单, 需要的朋友可以参考下。
index.asp 页面代码
<!-#Sertakan file = conn.asp->
<%
Setel cmd = conn.execute (pilih BigClassid, BigClassName dari BigClass)
tempid = cmd (bigclassid)
%>
<Pilih Nama = Menu Onchange = GetSubCategory (this.Value);>
<%
Jika tidak cmd.eof maka
lakukan saat bukan cmd.eof
BigClassid = CMD (BigClassID)
BigClassName = CMD (BigClassName)
%>
<Nilai opsi = <%= BigClassID%>> <%= BigClassName%> </ption>
<%
cmd.movenext
lingkaran
akhiri jika
cmd.close
atur cmd = tidak ada
%>
</pilih>
<Div ID = Subclass>
<Pilih Nama = Subwenu>
<%
Setel cxd = conn.execute (pilih * dari smallclass di mana bigclassid = & tempid)
Jika tidak cxd.eof maka
lakukan sementara bukan cxd.eof
smallclassid = cxd (smallclassid)
SmallClassName = CXD (SmallClassName)%>
<Nilai opsi = <%= SmallClassID%>> <%= SmallClassName%> </potion>
<%
cxd.movenext
lingkaran
cxd.close
atur cxd = tidak ada
kalau tidak
html = <pilih name = 'smallclassId'> <option value = '0' dipilih> 暂无小类 </pection> </ pilih>
Response.write html
akhiri jika
%>
</pilih>
</div>
Ajax.js 代码
// Dokumen JavaScript
fungsi createxmlhttp ()
{
xmlhttpobj = false;
mencoba{
xmlHttpoBj = xmlhttpRequest baru;
} catch (e) {
mencoba{
xmlhttpobj = ActivexObject baru (msxml2.xmlhttp);
} catch (e2) {
mencoba{
xmlhttpoBj = ActivexObject baru (microsoft.xmlhttp);
} catch (e3) {
xmlhttpobj = false;
}
}
}
return xmlhttpobj;
}
fungsi getSubCategory (BigClassId) {
if (bigclassid == 0) {
document.geteLementById (subkelas) .innerHtml = <pilih name = 'smallclassId'> <option value = '0' dipilih> 选择二级分类 </pection> </ pilih>;
kembali;
};
var xmlhttpobj = createxmlhttp ();
if (xmlhttpobj) {// 如果创建对象 xmlhttpoBj 成功
xmlhttpobj.onreadystatechange = handle;
xmlhttpobj.open ('get', getsubcategory.asp? bigclassid =+bigclassid+& angka =+math.random (), true); // dapatkan 方法 加个随机数。 加个随机数。
xmlhttpobj.send (null);
}
}
function handle () {// 客户端监控函数
//if(xmlHttpoBj.readyState==4) {// 服务器处理请求完成
if (xmlhttpobj.status == 200) {
// peringatan ('ok');
var html = xmlhttpobj.responsetext; // 获得返回值
document.geteLementById (subkelas) .innerHtml = html;
}kalau tidak{
document.geteLementById (subkelas) .innerHtml = 对不起 , 您请求的页面有问题 ...;
}
//}
//kalau tidak{
//document.getElementById(subclass).innerHtml=XMlHttpoBj.readyState;// 服务器处理中
//}
//}
}
getsubcategory.asp 代码
<%@language = vbscript codepage = 936%>
<!-#Sertakan file = conn.asp->
<%
response.charset = GB2312
BigClassId = safe (request.queryString (BigClassid))
Jika BigClassid <> maka
Setel RE = RegExp baru
re.ignorecase = true
re.global = false
re.pattern = ^[0-9] {1,3} $
Jika tidak re.test (BigClassid) maka
respons.write 非法参数
respons.end
akhiri jika%>
<%pada kesalahan resume berikutnya
Set P = Conn.Execute (Pilih * dari SmallClass Where BigClassid = & BigClassid)
Jika err maka
err.clear
respons.write 查询出错
respons.end
akhiri jika
Jika bukan P.eof maka
html = <pilih name = 'select2'> & vbnewline
lakukan sementara bukan p.eof
html = html & <value option = '& p (smallclassid) &'> & p (smallclassName) & </pection> & vbnewline
p.movenext
lingkaran
html = html & </pilih>
kalau tidak
html = <pilih name = 'smallclassId'> <option value = '0' dipilih> 暂无小类 </pection> </ pilih>
akhiri jika
p.close
Setel p = tidak ada
Conn.Close
Setel Conn = Tidak Ada
Response.write html
html =
akhiri jika
%>