您的位置:首页 > Web前端 > JavaScript

JavaScript实现二级、多级(N级)联动下拉列表框更新版,支持IE6,FireFox,函数和类两种调用方式,支持到N级,非常通用。

2006-06-16 11:34 976 查看
自己花了不少时间整理、编写的东东。本来有点舍不得放出来。放出来了也就不加任何版权什么XX的了,省得有人删除麻烦:D

特点:通用性强、实现了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" onchange="alert('xx');">
</select>
</form>
<div id="times"></div>
</body>
</html>

三、不足的地方

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

2、修改时的回显

新增演示代码:四级联动的演示

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=796430
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐