JavaScript实现多级(N级)联动下拉列表框更新版,支持IE6,火狐

本文主要是介绍JavaScript实现多级(N级)联动下拉列表框更新版,支持IE6,火狐,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

特点:通用性强、实现了script和html分离

废话少说、文档就不给了。想研究代码的看我以前的一篇blog

一、文件及源码
cs.js
用函数和类两种方法实现,调用时只要调用一种就可以了。

<!--
/**
 * Function to setup a CascadeSelect
 * @version build 20060324
 * @author  KimSoft (jinqinghua [at] gmail.com)
 * @update
 */

/**
 * setupCascadeSelect
 * @param cascadeSelect select object
 * @param parent data index of parent
 * @param nodes array contains data with structure [[parent, text, value],[],...[]]
 * @param [optional] is onchange
 */
function setupCascadeSelect(cascadeSelect, parent, nodes, isOnChange) {
  if (isOnChange == null){
    isOnChange = false;
  }
  cascadeSelect.onchange = function (){
    setupCascadeSelect(this, this.options[this.selectedIndex].value, nodes, true);
  };
  cascadeSelect.getAttr = function (attrName) {
    return this[attrName] ? this[attrName] : this.getAttribute(attrName);
  };
  cascadeSelect.getElementById = function (id) {
    return this.form.elements[id] ? this.form.elements[id]: document.getElementById(id);
  };
  cascadeSelect.setDisplayStyle = function(value) {
    if (!this.multiple){
      this.style.display = value;
    }
    var subElement = this.getElementById(this.getAttr("subElement"));
    if (subElement != undefined){
      subElement.setDisplayStyle = this.setDisplayStyle;
    }
  };
  nodes.getChildNodesByParent = function (parent) {
    var childNodes = new Array();
    if (parent + "" == ""){
      return childNodes;
    }
    for (var i = 0; i < nodes.length; i++){
      if (nodes[i][0] != undefined && nodes[i][0] == parent){
        childNodes[childNodes.length] = nodes[i];
      }
    }
    return childNodes;
  }

  if(!isOnChange){
    cascadeSelect.options.length = 0;
    var defaultText = cascadeSelect.getAttr("defaultText");
    var defaultValue = cascadeSelect.getAttr("defaultValue");
    var selectedValue = cascadeSelect.getAttr("selectedValue");

    if (defaultText != undefined && defaultValue != undefined){
      cascadeSelect.options[cascadeSelect.options.length] = new Option(defaultText, defaultValue);
    }
    var childNodes = nodes.getChildNodesByParent(parent);
    for (var i = 0; i < childNodes.length; i++){
      cascadeSelect.options[cascadeSelect.options.length] = new Option(childNodes[i][1], childNodes[i][2]);
      if (selectedValue != undefined && selectedValue == childNodes[i][2]){
        cascadeSelect.selectedIndex = cascadeSelect.options.length - 1;
      }
    }
  }

  if (cascadeSelect.options.length > 0){
    cascadeSelect.setDisplayStyle("");
    var subElement = cascadeSelect.getElementById(cascadeSelect.getAttr("subElement"));
    if (subElement != undefined){
      setupCascadeSelect(subElement, cascadeSelect.options[cascadeSelect.selectedIndex].value, nodes, false);
    }
  } else {
    cascadeSelect.setDisplayStyle("none");
  }
}

/**
 * 以类的方法实现连动
 */
function CascadeSelect(element, parent, nodes){
  this.form = element.form;
  this.nodes = nodes;
  this.attributes = {
    "subElement"    : "subElement",
    "defaultText"   : "defaultText",
    "defaultValue"  : "defaultValue",
    "selectedValue" : "selectedValue"
  }
  this.build(element, parent, false);
}

CascadeSelect.prototype.getElementById = function (id) {
    return this.form.elements[id] ? this.form.elements[id]: document.getElementById(id);
};

CascadeSelect.prototype.getAttribute = function (element, attrName) {
    return element[attrName] ? element[attrName] : element.getAttribute(attrName);
};

CascadeSelect.prototype.getChildNodesByParent = function (parent) {
    var childNodes = new Array();
    if (parent + "" == ""){
      return childNodes;
    }
    for (var i = 0; i < this.nodes.length; i++){
      if (this.nodes[i][0] != undefined && this.nodes[i][0] == parent){
        childNodes[childNodes.length] = this.nodes[i];
      }
    }
    return childNodes;
  }

CascadeSelect.prototype.setDisplayStyle = function(element, value) {
  var cs = this;
    if (!element.multiple){
      element.style.display = value;
    }
    var subElement = this.getElementById(this.getAttribute(element, this.attributes["subElement"]));
    if (subElement != undefined){
      subElement.setDisplayStyle = function () {cs.setDisplayStyle;}
    }
  };

CascadeSelect.prototype.build = function (element, parent, isOnChange) {
  var cs = this;
  element.onchange = function () {
    cs.build(this, this.options[this.selectedIndex].value, true);
  }
  if(!isOnChange){
    element.options.length = 0;
    var defaultText = this.getAttribute(element, this.attributes["defaultText"]);
    var defaultValue = this.getAttribute(element, this.attributes["defaultValue"]);
    var selectedValue = this.getAttribute(element, this.attributes["selectedValue"]);
 //alert(selectedValue);

    if (defaultText != undefined && defaultValue != undefined){
      element.options[element.options.length] = new Option(defaultText, defaultValue);
    }
    var childNodes = this.getChildNodesByParent(parent);
    for (var i = 0; i < childNodes.length; i++){
      element.options[element.options.length] = new Option(childNodes[i][1], childNodes[i][2]);
   //alert("childNodes[i][2]:" + childNodes[i][2]);
      if (selectedValue != undefined && selectedValue == childNodes[i][2]){
  alert(selectedValue);
        element.selectedIndex = element.options.length - 1;
      }
    }
  }

  if (element.options.length > 0){
    this.setDisplayStyle(element, "");
    var subElement = this.getElementById(this.getAttribute(element, this.attributes["subElement"]));
    if (subElement != undefined){
      this.build(subElement, element.options[element.selectedIndex].value, false);
    }
  } else {
    this.setDisplayStyle(element, "none");
  }
}
//-->

二、demo加说明

demo_cs2.htm


<html>
<body>
<script language="JavaScript" type="text/javascript" src="cs.js"></script>
<script language="JavaScript" type="text/javascript">
<!--
/*
生成联动菜单数组,可以从数据库生成,形式如下[[parent, text, value], [parent, text, value]]
优化部分:
一、用数据直接量减少生成的字符,数据结构简单
二、parent,value部分尽量使用数字形式,value部分尽量使用单引号,以减少生成的数据大小
*/
var aryCertType = [
[0,'外语证书',671],
[71,'大学英语四级',672],
[71,'大学英语六级',673],
[71,'剑桥商务英语证书',674],
[71,'英语专业四级',675],
[71,'英语专业六级',676],
[71,'英语专业八级',677],
[71,'托福',678],
[71,'国际英文水平测试',679],
[671,'中级口译证书',680],
[671,'高级口译证书',681],
[671,'日语一级证书',682],
[671,'日语二级证书',683],
[671,'日语三级证书',684],
[671,'日语四级证书',685],
[671,'俄语四级证书',686],
[671,'俄语六级证书',687],
[671,'法语四级证书',688],
[671,'法语六级证书',689],
[671,'德语四级证书',690],
[671,'德语六级证书',691],
[671,'GRE',692],
[671,'GMAT',693],
[671,'FCE',694],
[671,'全国公共英语等级考试',695],
[0,'计算机证书',696],
[696,'全国计算机应用技术证书',697],
[696,'全国计算机软件技术资格与水平考试',698],
[696,'初级程序员',699],
[696,'程序员',700],
[696,'高级程序员',701],
[696,'系统分析员',702],
[696,'全国计算机等级一级',703],
[696,'全国计算机等级二级',704],
[696,'全国计算机等级三级A',705],
[696,'全国计算机等级四级',706],
[696,'微软认证专家',707],
[696,'微软认证系统工程师',708],
[696,'微软认证系统开发师',709],
[696,'Cisco认证网络规划和网络支持工程师',710],
[696,'Cisco认证网络专家',711],
[696,'Novell授权网络管理师',712],
[696,'Novell授权网络工程师',713],
[696,'Novell授权高级网络工程师',714],
[696,'Novell授权Internet专家',715],
[696,'Intel认证方案咨询专家',716],
[696,'SUN认证Java程序员',717],
[696,'3com认证网络大师',718],
[696,'Adobe认证专家',719],
[696,'Lotus专家认证应用开发师',720],
[696,'Lotus专家认证系统管理师',721],
[696,'Sybase认证考试',722],
[696,'Cisco认证网络支持工程师',723],
[696,'Cisco认证资深网络支持工程师',724],
[696,'Cisco认证网络设计工程师',725],
[696,'Cisco认证资深网络设计工程师',726],
[696,'计算机初级',727],
[696,'计算机中级',728],
[696,'网络初级证书',729],
[696,'全国计算机等级三级B',730],
[0,'会计证书',731],
[731,'注册会计师',732],
[731,'会计上岗证',733],
[731,'助理会计师',734],
[731,'中级会计师',735],
[731,'高级会计师',736],
[731,'会计电算化证书',737],
[731,'国际财务会计证书',738],
[0,'职称证书',739],
[739,'初级工程师',740],
[739,'中级工程师',741],
[739,'高级工程师',742],
[739,'助理工程师',743],
[739,'初级经济师',744],
[739,'中级经济师',745],
[739,'高级经济师',746],
[740,'助理经济师',747]
]

/*
文档加载完成时给控件加载自定义属性
subElement:    可选的。指定一个子对象,必须为一个HTML Select控件 
selectedValue: 可选的。指定选定的值,在回显示时非常方便
defaultText:   可选的。默认文本
defaultValue:  可选的。默认值
优化的好处:
一、集中到Script里,方便管理
二、防止一些编辑器报告讨厌的XX标准不支持此属性的警告
三、让网页通过标准验证
*/
window.onload = function(){
  form = document.forms[0];
  form.certType.setAttribute("subElement", "certName");
  //form.certType.setAttribute("selectedValue", 739);
  form.certType.setAttribute("defaultText", "不限1级");
  form.certType.setAttribute("defaultValue", "");
 
  form.certName.setAttribute("selectedValue", 746);
  form.certName.setAttribute("defaultText", "不限2级");
  form.certName.setAttribute("defaultValue", "");

/*
调用
优化后的好处:
一、只要一个方法,调用方便,重用性高
二、优化了算法,执行速度快
*/
  //setupCascadeSelect(form.certType, 0, aryCertType, false);
  new CascadeSelect(form.certType, 0, aryCertType);
}
-->
</script>
<form name="form1" method="post" action="">
  <select name="certType">
  </select>
  <select name="certName" οnchange="alert('xx');">
  </select>
</form>
<div id="times"></div>
</body>
</html>

三、不足的地方

1、onchange事件被重定义了,如果想在onchage执行其它的东东,想办法吧。:)

2、修改时的回显

这篇关于JavaScript实现多级(N级)联动下拉列表框更新版,支持IE6,火狐的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/230121

相关文章

分布式锁在Spring Boot应用中的实现过程

《分布式锁在SpringBoot应用中的实现过程》文章介绍在SpringBoot中通过自定义Lock注解、LockAspect切面和RedisLockUtils工具类实现分布式锁,确保多实例并发操作... 目录Lock注解LockASPect切面RedisLockUtils工具类总结在现代微服务架构中,分布

Java使用Thumbnailator库实现图片处理与压缩功能

《Java使用Thumbnailator库实现图片处理与压缩功能》Thumbnailator是高性能Java图像处理库,支持缩放、旋转、水印添加、裁剪及格式转换,提供易用API和性能优化,适合Web应... 目录1. 图片处理库Thumbnailator介绍2. 基本和指定大小图片缩放功能2.1 图片缩放的

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

Redis客户端连接机制的实现方案

《Redis客户端连接机制的实现方案》本文主要介绍了Redis客户端连接机制的实现方案,包括事件驱动模型、非阻塞I/O处理、连接池应用及配置优化,具有一定的参考价值,感兴趣的可以了解一下... 目录1. Redis连接模型概述2. 连接建立过程详解2.1 连php接初始化流程2.2 关键配置参数3. 最大连

Python实现网格交易策略的过程

《Python实现网格交易策略的过程》本文讲解Python网格交易策略,利用ccxt获取加密货币数据及backtrader回测,通过设定网格节点,低买高卖获利,适合震荡行情,下面跟我一起看看我们的第一... 网格交易是一种经典的量化交易策略,其核心思想是在价格上下预设多个“网格”,当价格触发特定网格时执行买

python设置环境变量路径实现过程

《python设置环境变量路径实现过程》本文介绍设置Python路径的多种方法:临时设置(Windows用`set`,Linux/macOS用`export`)、永久设置(系统属性或shell配置文件... 目录设置python路径的方法临时设置环境变量(适用于当前会话)永久设置环境变量(Windows系统

Python对接支付宝支付之使用AliPay实现的详细操作指南

《Python对接支付宝支付之使用AliPay实现的详细操作指南》支付宝没有提供PythonSDK,但是强大的github就有提供python-alipay-sdk,封装里很多复杂操作,使用这个我们就... 目录一、引言二、准备工作2.1 支付宝开放平台入驻与应用创建2.2 密钥生成与配置2.3 安装ali

Spring Security 单点登录与自动登录机制的实现原理

《SpringSecurity单点登录与自动登录机制的实现原理》本文探讨SpringSecurity实现单点登录(SSO)与自动登录机制,涵盖JWT跨系统认证、RememberMe持久化Token... 目录一、核心概念解析1.1 单点登录(SSO)1.2 自动登录(Remember Me)二、代码分析三、

PyCharm中配置PyQt的实现步骤

《PyCharm中配置PyQt的实现步骤》PyCharm是JetBrains推出的一款强大的PythonIDE,结合PyQt可以进行pythion高效开发桌面GUI应用程序,本文就来介绍一下PyCha... 目录1. 安装China编程PyQt1.PyQt 核心组件2. 基础 PyQt 应用程序结构3. 使用 Q

Python实现批量提取BLF文件时间戳

《Python实现批量提取BLF文件时间戳》BLF(BinaryLoggingFormat)作为Vector公司推出的CAN总线数据记录格式,被广泛用于存储车辆通信数据,本文将使用Python轻松提取... 目录一、为什么需要批量处理 BLF 文件二、核心代码解析:从文件遍历到数据导出1. 环境准备与依赖库