自己花了不少時間整理、編寫的東東。本來有點捨不得放出來。放出來了也就不加任何著作權什麼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、修改時的回顯
新增示範代碼:四級聯動的示範
四、新增線上示範(2006/06/18)