ajax实现的级联菜单

来源:互联网 发布:mac 还原word文档 编辑:程序博客网 时间:2024/05/03 05:41
什么是级联菜单不用解释了,下面实现异步请求的方式实现实现二级联动;
ajax实现的级联菜单      ajax实现的级联菜单
 页面显示代码
   <selectname="chapter" id="curchapter"onchange="getnodelist()">
      <option>新建</option>
       <c:forEach items="${chapterlist}" var="chapter">
<option value="${chapter.chapterid}">${chapter.chaptername}</option>
</c:forEach>
   </select>
   请选择市:<select name="node"id="node"></select>
js代码:得到XMLHttpRequest对象过程已经省略:请参考                   http://blog.sina.com.cn/s/blog_bfd7909001016w9x.html 
  xmlhttp.onreadystatechange = function(){
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").options.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();