基于 ASP+AJAX 和数据库驱动的二级联动菜单, 需要的朋友可以参考下。
index.asp 页面代码
<!-#รวมไฟล์ = conn.asp->
-
ตั้งค่า cmd = conn.execute (เลือก bigclassid, bigclassname จาก bigclass)
tempid = cmd (bigclassid)
-
<เลือกชื่อ = เมนู onChange = getSubCategory (this.value);>
-
ถ้าไม่ cmd.eof แล้ว
ทำในขณะที่ไม่ใช่ cmd.eof
bigclassid = cmd (bigclassid)
bigclassName = cmd (bigclassname)
-
<ตัวเลือกค่า = <%= bigclassid%>> <%= bigclassName%> </portion>
-
cmd.movenext
วง
สิ้นสุดถ้า
cmd.close
ตั้งค่า cmd = ไม่มีอะไร
-
</เลือก>
<div id = subclass>
<select name = submenu>
-
ตั้งค่า cxd = conn.execute (เลือก * จาก smallclass ที่ bigclassid = & tempid)
ถ้าไม่ใช่ cxd.eof แล้ว
ทำในขณะที่ไม่ใช่ cxd.eof
smallClassID = CXD (smallClassID)
smallClassName = CXD (smallClassName)%>
<ตัวเลือกค่า = <%= smallclassId%>> <%= smallclassName%> </portion>
-
CXD.Movenext
วง
cxd.close
ตั้งค่า cxd = ไม่มีอะไร
อื่น
html = <select name = 'smallclassId'> <optural value = '0' เลือก> 暂无小类 </opotion> </select>
Response.write html
สิ้นสุดถ้า
-
</เลือก>
</div>
ajax.js 代码
// เอกสาร JavaScript
ฟังก์ชั่น createxmlhttp ()
-
xmlhttpoBj = false;
พยายาม{
xmlhttpoBj = ใหม่ xmlhttprequest;
} catch (e) {
พยายาม{
xmlhttpoBj = new ActiveXObject (msxml2.xmlhttp);
} catch (e2) {
พยายาม{
XMLHTTPOBJ = ใหม่ ActiveXObject (Microsoft.xmlhttp);
} catch (e3) {
xmlhttpoBj = false;
-
-
-
ส่งคืน xmlhttpobj;
-
ฟังก์ชั่น getSubCategory (bigclassid) {
if (bigclassid == 0) {
document.getElementById (subclass) .innerhtml = <select name = 'smallClassId'> <ค่าตัวเลือก = '0' เลือก> 选择二级分类 </ตัวเลือก> </เลือก>;
กลับ;
-
var xmlhttpoBj = createxmlhttp ();
if (xmlhttpoBj) {// 如果创建对象 xmlhttpoBj 成功
XMLHTTPOBJ.OnReadyStateChange = Handle;
xmlhttpoBj.open ('get', getSubcategory.asp? bigclassid =+bigclassid+& number =+math.random (), จริง); // รับ方法加个随机数。加个随机数。
XMLHTTPOBJ.SEND (NULL);
-
-
ฟังก์ชั่นที่จับ () {// 客户端监控函数
//if(xmlhttpobj.readystate==4) {// 服务器处理请求完成
if (xmlhttpoBj.status == 200) {
// แจ้งเตือน ('ตกลง');
var html = xmlhttpobj.responsetext; // 获得返回值
document.getElementById (subclass) .innerhtml = html;
}อื่น{
document.getElementById (subclass) .innerhtml = 对不起, 您请求的页面有问题 ... ;
-
-
//อื่น{
//document.getElementByid(SubClass).innerhtml=xmlhttpobj.readystate;
-
-
-
getSubcategory.asp 代码
<%@language = vbscript codepage = 936%>
<!-#รวมไฟล์ = conn.asp->
-
Response.charset = GB2312
bigclassid = safe (request.querystring (bigclassid))
ถ้า bigclassid <> แล้ว
ตั้งค่า re = ใหม่ regexp
re.ignorecase = true
re.global = false
re.pattern = ^[0-9] {1,3} $
ถ้าไม่ได้ทำการทดสอบ (bigclassid) แล้ว
Response.write 非法参数
Response.end
สิ้นสุดถ้า%>
<%เกี่ยวกับข้อผิดพลาดต่อไป
ตั้งค่า p = conn.execute (เลือก * จาก smallclass ที่ bigclassid = & bigclassid)
ถ้าเอ่อแล้ว
err.lear
Response.write 查询出错
Response.end
สิ้นสุดถ้า
ถ้าไม่ใช่ p.eof แล้ว
html = <select name = 'select2'> & vbNewline
ทำในขณะที่ไม่ใช่ p.eof
html = html & <ตัวเลือกค่า = '& p (smallclassId) &'> & p (smallclassName) & </options> & vbnewline
P.Movenext
วง
html = html & </select>
อื่น
html = <select name = 'smallclassId'> <optural value = '0' เลือก> 暂无小类 </opotion> </select>
สิ้นสุดถ้า
หน้าคลอด
ตั้งค่า p = ไม่มีอะไร
conn.close
ตั้งค่า conn = ไม่มีอะไร
Response.write html
html =
สิ้นสุดถ้า
-