近日,参考了几个版本的jsp+ajax制作的动态级联菜单,自己综合他们的优点封装了ajax层service层和dao层写了一个可以实现多种样式的级联菜单以供大家参考。
1。ajax.js
/** * Created by CECI.SH.DuanGH * User: Duan GaoHui * Date: 2008-6-26 * Time: 12:48:15 * */ function HttpRequest(id) { this.id = "HttpRequest_instance_" + id?id:"default"; this.request = false; this.responseText = false; this.responseXML = false; this.doGet = doGet; this.doPost = doPost; this.callback = null; this.readyStateChangeHandler = readyStateChangeHandler; if (window.ActiveXObject) { // For Internet Explorer on Windows try { this.request = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { this.request = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { this.request = false; } } //FIREFOX, AND OTHER BROWSER } else if (window.XMLHttpRequest) { try { this.request = new XMLHttpRequest(); } catch (e) { this.request = false; } } window[this.id] = this;
function doGet(url, handler) { if (this.request) { if (handler) { this.callback = handler; this.request.open('GET', url, true); this.request.setRequestHeader("If-Modified-Since", "0"); this.request.onreadystatechange = new Function(this.id + ".readyStateChangeHandler();"); this.request.send(null); } else { // Synchronous request, wait till we have it all this.request.open('GET', url, false); this.request.setRequestHeader("If-Modified-Since", "0"); this.request.send(null); this.responseText = this.request.responseText; //added by hanst, 2008-06-26,for xml document this.responseXML = this.request.responseXML; } } } //请求状态 判断是否响应成功 function readyStateChangeHandler() { if (this.request.readyState == 4) { if (this.request.status == 200) { this.responseText = this.request.responseText; //added by hanst , for xml document response, 2008-06-26, __start this.responseXML = this.request.responseXML; //added by hanst , for xml document response, 2008-06-26, __end this.callback(); } else { alert("DuanGh Ajax Error: HTTP status " + this.request.status); } } } //发送请求 函数 function doPost(url, f) { if (this.request) { this.request.open('POST', url, false); this.request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); this.request.send(formToContent(f)); this.responseText = this.request.responseText; } } //多功能 级联 菜单 能实现很多形式,至于service层怎么写,你看着模仿吧! function formToContent(f) { var sb = ""; var o, tagName, type, n, v; for (var i = 0; i < f.length; i++) { o = f[i]; tagName = o.tagName.toUpperCase(); type = o.type.toLowerCase(); ///alert(tagName + "," + type); n = o.name; switch (tagName) { case "INPUT": switch (type) { case "radio": if (o.length) { for (var j = 0; j < o.length; j++) { if (o[j].checked) { sb += "&" + encodeURIComponent(o.name) + "=" + encodeURIComponent(o[j].value); } } } else { if (o.checked) { sb += "&" + encodeURIComponent(o.name) + "=" + encodeURIComponent(o.value); } } break; case "checkbox": if (o.length) { for (var j = 0; j < o.length; j++) { if (o[j].checked) { sb += "&" + encodeURIComponent(o.name) + "=" + encodeURIComponent(o[j].value); } } } else { if (o.checked) { sb += "&" + encodeURIComponent(o.name) + "=" + encodeURIComponent(o.value); } } break; case "file": //to prevent file to be parsed as string break; default: sb += "&" + encodeURIComponent(o.name) + "=" + encodeURIComponent(o.value); } break; case "SELECT": sb += "&" + encodeURIComponent(o.name) + "=" + encodeURIComponent(o.options[o.selectedIndex].value); break; case "TEXTAREA": sb += "&" + encodeURIComponent(o.name) + "=" + encodeURIComponent(o.value); break; default: //do nothing } } //alert(sb.substring(1)); return sb; } }
//发送xmlHttpRequest请求,解析返回的xml function makeXmlRequest(url, selectName) { var req = new HttpRequest(); req.doGet(url); var xmlDoc = req.responseXML.documentElement; //取得XML中的关联关系节点 var xSel = xmlDoc.getElementsByTagName('select'); buildOptions(xSel, selectName); }
//根据解析的对象构造关联下拉框 function buildOptions(xSel, selectName) { //取得关联下拉框对象 var select2Obj = document.getElementById(selectName); select2Obj.options.length = 0; //option的value var xValue = null; //option的text var xText = null; //option var option = null;
//设置关联下拉框的值 for (var i = 0; i < xSel.length; i ++) { //二级联动 if (xSel[i].childNodes.length == 0) { xValue = xSel[i].attributes[0].value; xText = xSel[i].attributes[1].value; option = new Option(xText, xValue); try { select2Obj.add(option); } catch(e) { } } //多级联动可以自行继续构造 } } var v2Ajax = new HttpRequest("global");