ASP+AJAX 和数据库驱动的二级联动菜单, 需要的朋友可以参考下。
index.asp 页面代码
<!-#포함 파일 = conn.asp->
<%
cmd = conn.execute set (bigclass, bigclassname을 선택)
Tempid = cmd (bigclassid)
%>
<name = menu onchange = getSubcategory (this.value);>
<%
CMD.eof가 아니라면
cmd.eof가 아닙니다
bigclassid = cmd (bigclassid)
bigclassname = cmd (bigclassname)
%>
<옵션 값 = <%= bigclassid%>> <%= bigclassName%> </옵션>
<%
cmd.movenext
고리
끝 If
cmd.close
CMD = 아무것도 설정하지 않습니다
%>
</선택>
<div id = 하위 클래스>
<name = submenu 선택을 선택하십시오
<%
cxd = conn.execute를 설정합니다 (bigclassid = & tempid에서 smallclass에서 * select *)
CXD.eof가 아니라면
cxd.eof가 아닙니다
smallclassid = cxd (smallclassid)
smallclassname = cxd (smallclassname)%>
<옵션 값 = <%= smallclassid%>> <%= smallclassname%> </옵션>
<%
cxd.movenext
고리
CXD.CLOSE
CXD = 아무것도 설정하지 않습니다
또 다른
html = <select name = 'smallclassid'> <옵션 value = '0'selected> 暂无小类 </옵션> </select>
응답. html
끝 If
%>
</선택>
</div>
ajax.js.
// JavaScript 문서
함수 createxmlhttp ()
{
xmlhttpobj = false;
노력하다{
xmlhttpobj = 새로운 xmlhttprequest;
} catch (e) {
노력하다{
xmlhttpobj = new ActiveXobject (msxml2.xmlhttp);
} catch (e2) {
노력하다{
xmlhttpobj = new ActiveXobject (microsoft.xmlhttp);
} catch (e3) {
xmlhttpobj = false;
}
}
}
xmlhttpobj를 반환합니다.
}
함수 getSubcategory (bigclassid) {
if (bigclassid == 0) {
document.getElementById (서브 클래스) .innerHtml = <select name = 'smallclassid'> <옵션 값 = '0'selected> </옵션> </select>;
반품;
};
var xmlhttpobj = createxmlhttp ();
if (xmlhttpobj) {// 如果创建对象 xmlhttpobj 成功
xmlhttpobj.onreadyStateChange = 핸들;
xmlhttpobj.open ( 'get', getsubcategory.asp? bigclassid =+bigclassid+& number =+math.random (), true); // get 方法 加个随机数。 加个随机数。
xmlhttpobj.send (null);
}
}
함수 핸들 () {// 客户端监控函数
//if(xmlhttpobj.readystate= 4) {// 服务器处理请求完成
if (xmlhttpobj.status == 200) {
// Alert ( 'Ok');
var html = xmlhttpobj.responsetext; // 获得返回值
document.getElementById (서브 클래스) .innerHtml = html;
}또 다른{
document.getElementById (서브 클래스) .innerHtml = 对不起, 您请求的页面有问题 ...;
}
//}
//또 다른{
//document.getElementById(subclass).innerhtml=xmlhttpobj.readystate ;// 服务器处理中
//}
//}
}
getSubcategory.asp 代码
<%@language = vbscript codepage = 936%>
<!-#포함 파일 = conn.asp->
<%
응답 .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)가 아닌 경우
응답. 쓰기 非法参数
응답. 엔드
%>를 종료하십시오
다음에 오류 이력서에서 <%
p = conn.execute를 설정합니다 (bigclassid = & bigclassid where smallclass에서 * 선택)
그렇다면
err.clear
응답. 쓰기 查询出错
응답. 엔드
끝 If
그렇다면 그렇지 않다면
html = <select name = 'select2'> & vbnewline
p.eof가 아닌 동안
html = html & <옵션 value = '& p (smallclassid) &'> & p (smallclassname) & </옵션> & vbnewline
p.movenext
고리
html = html & </select>
또 다른
html = <select name = 'smallclassid'> <옵션 value = '0'selected> 暂无小类 </옵션> </select>
끝 If
p.close
p = 아무것도 설정하지 않습니다
conn.close
Conn = 아무것도 설정하지 않습니다
응답. html
html =
끝 If
%>