本文主要是介绍ajax实现的级联菜单,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
什么是级联菜单不用解释了,下面实现异步请求的方式实现实现二级联动;

页面显示代码 : <select name="chapter" id="curchapter" οnchange="getnodelist()"> <option>新建</option> <c:forEach items="${chapterlist }" var="chapter"> </select> 请选择市:<select name="node" id="node"></select> xmlhttp.onreadystatechange = function() {


<option value="${chapter.chapterid }">${chapter.chaptername}</option>
</c:forEach>
js代码:得到XMLHttpRequest对象过程已经省略:请参考 http://blog.sina.com.cn/s/blog_bfd7909001016w9x.html
if (4 == xmlhttp.readyState) {
if (200 == xmlhttp.status) {
var temp = xmlhttp.responseText;
var arr = temp.split("|");
document.getElementByIdx_x_x_x_x_x_x_x("node").opti ons.length = 0;
//长度设置为0即可
for ( var i = 0; i < arr.length; i++) {
var arg = arr[i].split(",");
var opp = new Option(arg[1], arg[0]);
document.getElementByIdx_x_x_x("node").options.add(opp,null );
}
} else {
//alert("获取章节信息失败啦");
}
}
};
xmlhttp.open("post", "getnode", true);
xmlhttp.setRequestHeader('Content-type','application/x-www-form- urlencoded');
var paras = "chaptetid=" + document.getElementByIdx_x_x_x("curchapter").value;
xmlhttp.send(paras);//发送的是查询字符串
}
注:请求服务端发送的数据的格式为:
result=city.getId()+","+city.getName()+"|"+city2.getId()+","+city2.getName();
这篇关于ajax实现的级联菜单的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!