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

相关文章

SpringBoot集成redisson实现延时队列教程

《SpringBoot集成redisson实现延时队列教程》文章介绍了使用Redisson实现延迟队列的完整步骤,包括依赖导入、Redis配置、工具类封装、业务枚举定义、执行器实现、Bean创建、消费... 目录1、先给项目导入Redisson依赖2、配置redis3、创建 RedissonConfig 配

Python的Darts库实现时间序列预测

《Python的Darts库实现时间序列预测》Darts一个集统计、机器学习与深度学习模型于一体的Python时间序列预测库,本文主要介绍了Python的Darts库实现时间序列预测,感兴趣的可以了解... 目录目录一、什么是 Darts?二、安装与基本配置安装 Darts导入基础模块三、时间序列数据结构与

Python使用FastAPI实现大文件分片上传与断点续传功能

《Python使用FastAPI实现大文件分片上传与断点续传功能》大文件直传常遇到超时、网络抖动失败、失败后只能重传的问题,分片上传+断点续传可以把大文件拆成若干小块逐个上传,并在中断后从已完成分片继... 目录一、接口设计二、服务端实现(FastAPI)2.1 运行环境2.2 目录结构建议2.3 serv

C#实现千万数据秒级导入的代码

《C#实现千万数据秒级导入的代码》在实际开发中excel导入很常见,现代社会中很容易遇到大数据处理业务,所以本文我就给大家分享一下千万数据秒级导入怎么实现,文中有详细的代码示例供大家参考,需要的朋友可... 目录前言一、数据存储二、处理逻辑优化前代码处理逻辑优化后的代码总结前言在实际开发中excel导入很

SpringBoot+RustFS 实现文件切片极速上传的实例代码

《SpringBoot+RustFS实现文件切片极速上传的实例代码》本文介绍利用SpringBoot和RustFS构建高性能文件切片上传系统,实现大文件秒传、断点续传和分片上传等功能,具有一定的参考... 目录一、为什么选择 RustFS + SpringBoot?二、环境准备与部署2.1 安装 RustF

Nginx部署HTTP/3的实现步骤

《Nginx部署HTTP/3的实现步骤》本文介绍了在Nginx中部署HTTP/3的详细步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录前提条件第一步:安装必要的依赖库第二步:获取并构建 BoringSSL第三步:获取 Nginx

MyBatis Plus实现时间字段自动填充的完整方案

《MyBatisPlus实现时间字段自动填充的完整方案》在日常开发中,我们经常需要记录数据的创建时间和更新时间,传统的做法是在每次插入或更新操作时手动设置这些时间字段,这种方式不仅繁琐,还容易遗漏,... 目录前言解决目标技术栈实现步骤1. 实体类注解配置2. 创建元数据处理器3. 服务层代码优化填充机制详

Python实现Excel批量样式修改器(附完整代码)

《Python实现Excel批量样式修改器(附完整代码)》这篇文章主要为大家详细介绍了如何使用Python实现一个Excel批量样式修改器,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一... 目录前言功能特性核心功能界面特性系统要求安装说明使用指南基本操作流程高级功能技术实现核心技术栈关键函

Java实现字节字符转bcd编码

《Java实现字节字符转bcd编码》BCD是一种将十进制数字编码为二进制的表示方式,常用于数字显示和存储,本文将介绍如何在Java中实现字节字符转BCD码的过程,需要的小伙伴可以了解下... 目录前言BCD码是什么Java实现字节转bcd编码方法补充总结前言BCD码(Binary-Coded Decima

Vue和React受控组件的区别小结

《Vue和React受控组件的区别小结》本文主要介绍了Vue和React受控组件的区别小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录背景React 的实现vue3 的实现写法一:直接修改事件参数写法二:通过ref引用 DOMVu