JavaScript: 复制代码 代码如下: <BR>var level1 = ["Beijing", "GuangZhou", "ShangHai"]; <BR>var level2 = [["ZhaoYang", "TianTan", "GuGong"], ["Tianhe", "Panyu"], ["PuDong", "PuXi"]]; <BR>var level3 = [[["TianShan", "HuangShan"], ["TianTan1", "TianTan2"], ["GuGong1", "GuGong2", "GuGong3", "GuGong4"]], [["TianHe1", "TianHe2"], ["PanYu1", "PanYu2"]], [["PuDong1", "PuDong2"], ["PuXi1", "PuXi2"]]]; <BR>var Levels = { <BR>fL: null,//用存储各级select的DOM引用 <BR>sL: null, <BR>tL: null, <BR>l1: null, <BR>l2: null, <BR>l3: null, <BR>$: function(id){ <BR>return (typeof id == "object") ? id : document.getElementById(id); <BR>}, <BR>init: function(fID, sID, tID, l1, l2, l3){ <BR>this.fL = this.$(fID); <BR>this.sL = this.$(sID); <BR>this.tL = this.$(tID); <BR>this.l1 = l1; <BR>this.l2 = l2; <BR>this.l3 = l3; <BR>this.fL.options.add(new Option("Select",-1));//给一级菜单添加一个“select”标志 <BR>for (var i = 0; i < l1.length; i++) { <BR>var item = new Option(l1[i], i); <BR>this.fL.options.add(item); <BR>} <BR>this.doLev2(); //调用doLev2函数,处理二级菜单 <BR>this.doLev3(); //调用doLev3函数,处理三级菜单 <BR>}, <BR>removeSTL: function(){ //用于删除第二、三级的菜单项 <BR>this.sL.options.length = 0; <BR>this.tL.options.length = 0; <BR>}, <BR>removeTL: function(){ //用于删除第三级的菜单项 <BR>this.tL.options.length = 0; <BR>}, <BR>doLev2: function(){ //处理二级菜单 <BR>var that = this; <BR>this.fL.onchange = function(){ <BR>that.removeSTL(); //删除旧second的select <BR>if (that.fL.selectedIndex == 0) { <BR>that.removeSTL(); // //删除旧second的select <BR>} <BR>else { <BR>that.sL.options.add(new Option("Select", -1)); //给二级菜单添加一个“select”标志 <BR>//获取第二级所需的数组 <BR>var items = that.l2[that.fL.selectedIndex - 1]; <BR>for (var i = 0; i < items.length; i++) { //添加第二级的新select项 <BR>var item = new Option(items[i], i); <BR>that.sL.options.add(item); <BR>} <BR>} <BR>} <BR>}, <BR>doLev3: function(){ //处理三级菜单 <BR>var that = this; <BR>this.sL.onchange = function(){ <BR>that.removeTL(); //删除旧third的select <BR>if (that.sL.selectedIndex == 0) { <BR>that.removeTL(); //删除旧third的select <BR>} <BR>else { <BR>that.tL.options.add(new Option("Select", -1)); //给三级菜单添加一个“select”标志 <BR>//获取第三级所需的数组 <BR>var items = that.l3[that.fL.selectedIndex - 1][that.sL.selectedIndex - 1]; <BR>for (var i = 0; i < items.length; i++) { //添加第三级的新select项 <BR>var item = new Option(items[i], i); <BR>that.tL.options.add(item); <BR>} <BR>} <BR>} <BR>} <BR>} <BR>onload = function(){ <BR>Levels.init("first", "second", "third", level1,level2,level3); //调用Levels的init方法 <BR>} <BR> HTML: 复制代码 代码如下: 演示代码: Untitled Document var level1 = ["Beijing", "GuangZhou", "ShangHai"]; var level2 = [["ZhaoYang", "TianTan", "GuGong"], ["Tianhe", "Panyu"], ["PuDong", "PuXi"]]; var level3 = [[["TianShan", "HuangShan"], ["TianTan1", "TianTan2"], ["GuGong1", "GuGong2", "GuGong3", "GuGong4"]], [["TianHe1", "TianHe2"], ["PanYu1", "PanYu2"]], [["PuDong1", "PuDong2"], ["PuXi1", "PuXi2"]]]; var Levels = { fL: null,//ç¨åå¨å级selectçDOMå¼ç¨ sL: null, tL: null, l1: null, l2: null, l3: null, $: function(id){ return (typeof id == "object") ? id : document.getElementById(id); }, init: function(fID, sID, tID, l1, l2, l3){ this.fL = this.$(fID); this.sL = this.$(sID); this.tL = this.$(tID); this.l1 = l1; this.l2 = l2; this.l3 = l3; this.fL.options.add(new Option("Select",-1));//ç»ä¸çº§èåæ·»å ä¸ä¸ª"select"æ å¿ for (var i = 0; i < l1.length; i++) { var item = new Option(l1[i], i); this.fL.options.add(item); } this.doLev2(); //è°ç¨doLev2å½æ°ï¼å¤çäºçº§èå this.doLev3(); //è°ç¨doLev3å½æ°ï¼å¤çä¸çº§èå }, removeSTL: function(){ //ç¨äºå é¤ç¬¬äºãä¸çº§çèå项 this.sL.options.length = 0; this.tL.options.length = 0; }, removeTL: function(){ //ç¨äºå é¤ç¬¬ä¸çº§çèå项 this.tL.options.length = 0; }, doLev2: function(){ //å¤çäºçº§èå var that = this; this.fL.onchange = function(){ that.removeSTL(); //å é¤æ§secondçselect if (that.fL.selectedIndex == 0) { that.removeSTL(); // //å é¤æ§secondçselect } else { that.sL.options.add(new Option("Select", -1)); //ç»äºçº§èåæ·»å ä¸ä¸ª"select"æ å¿ //è·å第äºçº§æéçæ°ç» var items = that.l2[that.fL.selectedIndex - 1]; for (var i = 0; i < items.length; i++) { //æ·»å 第äºçº§çæ°select项 var item = new Option(items[i], i); that.sL.options.add(item); } } } }, doLev3: function(){ //å¤çä¸çº§èå var that = this; this.sL.onchange = function(){ that.removeTL(); //å é¤æ§thirdçselect if (that.sL.selectedIndex == 0) { that.removeTL(); //å é¤æ§thirdçselect } else { that.tL.options.add(new Option("Select", -1)); //ç»ä¸çº§èåæ·»å ä¸ä¸ª"select"æ å¿ //è·å第ä¸çº§æéçæ°ç» var items = that.l3[that.fL.selectedIndex - 1][that.sL.selectedIndex - 1]; for (var i = 0; i < items.length; i++) { //æ·»å 第ä¸çº§çæ°select项 var item = new Option(items[i], i); that.tL.options.add(item); } } } } } onload = function(){ Levels.init("first", "second", "third", level1,level2,level3); //è°ç¨Levelsçinitæ¹æ³ } [Ctrl+A å ¨é 注:å¦éå¼å ¥å¤é¨Jséå·æ°æè½æ§è¡]