Die mehrstufige Verknüpfung kann durch direktes Kopieren des Codes ausgeführt werden. Reines Front-End ohne Back-End und Codekomprimierungspaket ist unten enthaltenhtml PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><br>
<br>
<kopf><br>
<meta>
<br>
<title>JavaScript 自定义多级联动下拉菜单</title>
<br>
<script>var $ = function(id) {<br />
<br />
return "string" == typeof id ? document.getElementById(id) : id;<br />
<br />
};<br />
<br />
Funktion addEventHandler(oTarget, sEventType, fnHandler) {<br />
<br />
if (oTarget.addEventListener) {<br />
<br />
oTarget.addEventListener(sEventType, fnHandler, false);<br />
<br />
} else if (oTarget.attachEvent) {<br />
<br />
oTarget.attachEvent("on" sEventType, fnHandler);<br />
<br />
} else {<br />
<br />
oTarget["on" sEventType] = fnHandler;<br />
<br />
}<br />
<br />
};<br />
<br />
Funktion Each(arrList, fun) {<br />
<br />
für (var i = 0,<br />
len = arrList.length; ich < len; i ) {<br />
fun(arrList[i], i);<br />
}<br />
<br />
};<br />
<br />
Funktion GetOption(val, txt) {<br />
<br />
var op = document.createElement("OPTION");<br />
<br />
op.value = val;<br />
op.innerHTML = txt;<br />
<br />
return op;<br />
<br />
};<br />
<br />
var Class = {<br />
<br />
create: function() {<br />
<br />
return function() {<br />
<br />
this.initialize.apply(this, arguments);<br />
<br />
}<br />
<br /> }<br />
<br />
}<br />
<br />
Object.extend = function(destination, source) {<br />
<br />
für (Var Eigenschaft in Quelle) {<br />
<br />
Ziel[Eigenschaft] = Quelle[Eigenschaft];<br />
<br />
}<br />
<br />
Rückflugziel;<br />
<br />
}<br />
<br />
var CascadeSelect = Class.create();<br />
<br />
CascadeSelect.prototype = {<br />
<br />
//select集合,菜单对象<br />
initialisieren: function(arrSelects, arrMenu, options) {<br />
<br />
if (arrSelects.length <= 0 || arrMenu.lenght <= 0) return; //菜单对象<br />
<br />
var oThis = this;<br />
<br />
this.Selects = []; //select集合<br />
this.Menu = arrMenu; //菜单对象<br />
<br />
this.SetOptions(options);<br />
<br />
this.Default = this.options.Default || [];<br />
<br />
this.ShowEmpty = !!this.options.ShowEmpty;<br />
<br />
this.EmptyText = this.options.EmptyText.toString();<br />
<br />
//设置Selects集合和change事件<br />
Each(arrSelects,<br />
Funktion(o, i) {<br />
<br />
addEventHandler((oThis.Selects[i] = $(o)), "change",<br />
function() {<br />
oThis.Set(i);<br />
});<br />
<br />
});<br />
<br />
this.ReSet();<br />
<br />
},<br />
<br />
//设置默认属性<br /> SetOptions: function(options) {<br />
<br />
This.options = { //Standardwert<br />
Standard: [],<br />
//Standardwert (in der Reihenfolge)<br />
ShowEmpty: false,<br />
EmptyText: „Bitte auswählen“ //Anzeigetext für leeren Wert (gültig, wenn ShowEmpty wahr ist)<br />
};<br />
<br />
Object.extend(this.options, options || {});<br />
<br />
},<br />
<br />
Zurücksetzen: function() {<br />
<br />
This.SetSelect(this.Selects[0], this.Menu, this.Default.shift());<br />
<br />
This.Set(0);<br />
<br />
},<br />
<br />
// Alle Einstellungen auswählen <br />
Set: function(index) {<br />
<br />
var menu = this.Menu<br />
<br />
//Die erste Auswahl muss nicht verarbeitet werden, daher beginnt sie bei 1<br />
für (var i = 1,<br />
Len = this.selects.length; i & lt; i) {<br />
<br />
Wenn (menu.length > 0) {<br />
<br />
var value = this.Selects[i - 1].value;<br />
<br />
If (value != "") {<br />
<br />
Jedes(Menü,<br />
Funktion(o) {<br />
If (o.val == value) {<br />menu = o.menu ||. [];<br />
}
});
<br />
} Sonst {<s>
Menü = [];<br />
}
<br />
If (i > index) {<br />
This.SetSelect(this.Selects[i], menu, this.Default.shift());<br />
}
<br />
} Sonst {<s>
<br />
This.SetSelect(this.Selects[i], [], "");<br />
<br />
}
<br />
}<br />
<br />
This.Default.length = 0;<br />
<br />
},<br />
<br />
//Einstellungen auswählen<br />
SetSelect: function(oSel, menu, value) {<br />
<br />
oSel.options.length = 0;<br />
oSel.disabled = false;<br />
<br />
If (this.ShowEmpty) {<br />
oSel.appendChild(GetOption("", this.EmptyText));<br />
}<br />
<br />
If (menu.length <= 0) {<br />
oSel.disabled = true;<br /> zurück ; <br />
}<br />
<br />
Jedes (Menü, <br />
Funktion ( o ) { <br />
<br />
var op = GetOption(o.val, o.txt ? o.txt: o.val);<br />
op.selected = (value == op.value);<br />
<br />
oSel. appendChild (up);
<br />
});<br />
<br />
},<br />
<br />
//Unterhaltung<br />
Hinzufügen: Funktion (Menü) { <br />
<br />
this.Menu[this.Menu.length] = menu;<br />
<br />
this.ReSet();<br />
<br />
},<br />
<br />
//Englische Unterhaltung<br />
Löschen: Funktion (index) { <br />
<br />
if (index < 0 || index > = this. Menu. length) return;
<br />
for ( var i = index , <br />
len = this.Menu.length -- 1; ich < len; i ) {<br />
this.Menu[i] = this.Menu[i1];<br />
}<br />
<br />
this.Menu.pop()<br />
<br />
this.ReSet();<br />
<br />
}<br />
<br />
};<br />
<br />
Fenster . onload = function ( ) { <br />
<br />
var menu = [<br />
<br />
{<br />
'val': '1',<br />
'txt': 'value'<br />
},<br />
<br />
{<br />
'val': '2 ->',<br /> 'Menü': [<br />
<br />
{<br />
'val': '2_1'<br />
},<br />
<br />
{<br />
'val': '2_2'<br />
}<br />
<br />
]<br />
},<br />
<br />
{<br />
'val':'3 ->',<br />
'Menü': [<br />
<br />
{<br />
'val':'3_1 ->',<br />
'Menü': [<br />
<br />
{<br />
'val': '3_1_1'<br />
},<br />
<br />
{<br />
'val': '3_1_2'<br />
}<br />
<br />
]<br />
},<br />
<br />
{<br />
'val': '3_2'<br />
}<br />
<br />
]<br />
},<br />
<br />
{<br />
'val':'4 ->',<br />
'Menü': [<br />
<br />
{<br /> 'val':'4_1 ->',<br />
'Menü': [<br />
<br />
{<br />
'val':'4_1_1 ->',<br />
'Menü': [<br />
<br />
{<br />
'val':'4_1_1_1'<br />
}<br />
<br />
]<br />
}<br />
<br />
]<br />
}<br />
<br />
]<br />
}<br />
<br />
];<br />
<br />
var sel = ["sel1", "sel2", "sel3", "sel4", "sel5"];<br />
<br />
var val = ["3 ->", "3_1 ->", "3_1_2"];<br />
<br />
var cs = new CascadeSelect(sel, menu, {<br />
Standard: Herbst<br />
});<br />
<br />
$("btnA").onclick = function() {<br />
cs.ShowEmpty = !cs.ShowEmpty;<br />
}<br />
<br />
$("btnB").onclick = function() {<br />
<br />
cs.Add(<br />
<br />
{<br />
'val':'5 ->',<br />
'Menü': [<br />
<br />
{<br /> 'val': '5_1 ->',<br />
'Menü': [<br />
<br />
{<br />
'val': '5_1_1 ->',<br />
'Menü': [<br />
<br />
{<br />
'val': '5_1_1_1 ->',<br />
'Menü': [<br />
<br />
{<br />
'val': '5_1_1_1_1'<br />
}<br />
<br />
]<br />
}<br />
<br />
]<br />
}<br />
<br />
]<br />
}<br />
<br />
]<br />
}<br />
<br />
)<br />
<br />
}<br />
<br />
$("btnC").onclick = function() {<br />
<br />
cs.Delete(3)<br />
<br />
}<br />
<br />
}</script><br>
<style>.sel select{ width:100px;}</style>
<br><br>
<div class="sel">
<br>
<select id="sel5"></select><br>
<input id="btnA" type="button" value="Leere Werte anzeigen/nicht anzeigen"><br>
<input type="button" value="Menü hinzufügen"><br>
<input id="btnC" type="button" value="Menü reduzieren">
</div>
<br>
<br>
</kopf>