javascript 三 级下拉选择菜单Levels对象
发布时间:2023-09-05 03:28:48
标签:javascript, , ,下拉选择,菜单
JavaScript:
<script type="text/javascript">
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方法
}
</script>
HTML:
<form>
<select id="first">
</select>
<select id="second">
</select>
<select id="third">
</select>
</form>
演示代码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>Untitled Document</title> <script type="text/javascript"> 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方法 } </script> </head> <body> <form> <select id="first"> </select> <select id="second"> </select> <select id="third"> </select> </form> </body> </html>


猜你喜欢
- create table test3(id int primary key not null identity(1,1),uname var
- Dean Edwards 最近有篇文章很精彩,忍不住在这里翻译下。-- Split --很多 Javascript 框架都提供了自定义事件(
- 1.当前时间戳转换为指定格式的日期# -*- coding: utf-8 -*-# @Time : 2019/5/31 10:5
- list.asp<%@LANGUAGE="VBSCRIPT" CODEPAGE="936&qu
- 一、问题描述当用JS调用form的方法submit直接提交form的时候,submit事件不响应。为什么?知道的请回复。类比一下,我用inp
- 1、按照javaweb项目的要求逐步建立搭建起机构,具体的类包有:model 、db、dao、test;具体的架构详见下图:2、根据搭建的项
- 第一种方法:A=[0]*8第二种方法:import numpy as np A=np.zeros(8)来源:https://blog.csd
- 本文实例讲述了vue实现父子组件之间的通信以及兄弟组件的通信功能。分享给大家供大家参考,具体如下:<!DOCTYPE html>
- Python文件遍历os.walk()与os.listdir()在图片处理过程中,样本数据的组织是个常见的问题,样本组织好了,后面数据转换、
- 如下所示:#彩色螺旋线import turtleturtle.pensize(2)turtle.bgcolor("black&qu
- 完整的Oracle数据库通常由两部分组成:Oracle数据库和数据库实例。 1) 数据库是一系列物理文件的集合(数据文件,控制文件,联机日志
- 一、触发器简介1、什么是触发器?触发器是与表有关的数据库对象,在满足定义条件时触发,并执行触发器中定义的语句集合。2、触发器的特性有begi
- Declaring class members or methods as st
- 本文详细介绍了网站的反爬虫策略,在这里把我写爬虫以来遇到的各种反爬虫策略和应对的方法总结一下。从功能上来讲,爬虫一般分为数据采集,处理,储存
- 说到排序,很多人可能第一想到的就是sorted,但是你可能不知道python中其实还有还就中方法哟,并且好多种场景下效率都会比sorted高
- 前言:python利用matplotlib库中的plt.ion()函数实现即时数据动态显示:1.非定长的时间轴代码示例:# -*- codi
- axios是通过Promise实现对ajax技术的一种封装,就像jquery对ajax的封装一样,axios回来的数据是promise,aj
- 本文实例讲述了Python的多态性。分享给大家供大家参考。具体如下:#!/usr/bin/env python# polymorphism
- 原文:Five quick JavaScript tips真是五个很quick的小提示:1.只在<form>元素上使用submi
- 1:开启binlog日志记录 修改mysql配置文件mysql.ini,在[mysqld]节点下添加 # log-bin log-bin =