标签:
本文主要探讨使用javascript实现二级联动菜单。众所周知,二级联动菜单在实际的开发中是比较常见的,虽说实现方式千差万别,但是,使用javascript对其进行实现是使用其他框架进行实现的基础。有必要研究探讨一下。
比如,常见的选择所属地区的2级联动菜单,当选择省份之后,2级菜单选项会随之变化。
<select id="city"></select> <select id="district"></select>
对1级菜单选项的数据使用1个数组
对2级菜单选项的数据使用1个二维数组来存放,并且,2个数组相应下标位置上的数据是对应的
//城市 var city = ['请选择','北京','天津','上海','重庆']; //地区 var district = [[], ['东城','西城','朝阳','海淀'], ['河东','河西','河北','南开'], ['黄埔','徐汇','长宁','静安'], ['渝北','渝中','江北','江津']];
window.onload=function(){ createCity(); document.getElementById("city").onchange= createDistrict; };
function createCity(){ //获得1级菜单select var ci = document.getElementById("city"); //为1级菜单select添加option for(var i in city){ var op = new Option(city[i],city[i]); ci.options.add(op); } }
document.getElementById("city").onchange= createDistrict;
function createDistrict(){ //获取当前选中的一级菜单的选项的下标 var index = document.getElementById("city").selectedIndex; //获得2级菜单select var di = document.getElementById("district"); //清空二级菜单选项 di.options.length=0; //为2级菜单select添加option for(var i in district[index]){ var op = new Option(district[index][i],district[index][i]); di.options.add(op); } }
submenu.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'submenu.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <script type="text/javascript"> //城市 var city = ['请选择','北京','天津','上海','重庆']; //地区 var district = [[], ['东城','西城','朝阳','海淀'], ['河东','河西','河北','南开'], ['黄埔','徐汇','长宁','静安'], ['渝北','渝中','江北','江津']]; window.onload=function(){ createCity(); document.getElementById("city").onchange= createDistrict; }; function createCity(){ //获得1级菜单select var ci = document.getElementById("city"); //为1级菜单select添加option for(var i in city){ var op = new Option(city[i],city[i]); ci.options.add(op); } } function createDistrict(){ //获取当前选中的一级菜单的选项的下标 var index = document.getElementById("city").selectedIndex; //获得2级菜单select var di = document.getElementById("district"); //清空二级菜单选项 di.options.length=0; //为2级菜单select添加option for(var i in district[index]){ var op = new Option(district[index][i],district[index][i]); di.options.add(op); } } </script> </head> <body> <p>请选择所述地区:</p> <select id="city"></select> <select id="district"></select> </body> </html>
运行效果
//获得2级菜单select var di = document.getElementById("district"); //清空二级菜单选项 di.options.length=0;
//城市 var city = ['请选择','北京','天津','上海','重庆']; //地区 var district = [[], ['东城','西城','朝阳','海淀'], ['河东','河西','河北','南开'], ['黄埔','徐汇','长宁','静安'], ['渝北','渝中','江北','江津']];以上即为使用javascript实现二级联动菜单的内容,在此基础上还可以实现更为复杂的多级菜单,以及在实际应用中结合ajax从后台数据库取出数据进行动态显示等。
标签:
原文地址:http://blog.csdn.net/u011526599/article/details/51340726