Wie man erstellt: Kaskadendropdown-Listen
- 上一页 单击下拉菜单
- 下一页 顶部导航栏中的下拉菜单
Lernen Sie, wie man mit JavaScript Kaskadendropdown-Listen erstellt.
Erstellen Sie drei Dropdown-Listen
Erstellen Sie drei Dropdown-Listen im HTML-Formular.
Die zweiten und dritten Dropdown-Listen zeigen verschiedene Optionen an, die von der Auswahl im übergeordneten Dropdown-Menü abhängen.
Erster Schritt - Hinzufügen von HTML:
<form name="form1" id="form1" action="/action_page.php"> Fächer: <select name="subject" id="subject"> <option value="" selected="selected">Wählen Sie das Fach aus</option> </select> <br><br> Themen: <select name="topic" id="topic"> <option value="" selected="selected">Bitte wählen Sie zunächst das Fach aus</option> </select> <br><br> Kapitel: <select name="chapter" id="chapter"> <option value="" selected="selected">Bitte wählen Sie zunächst das Thema aus</option> </select> <br><br> <input type="submit" value="Submit"> </form>
Zweiter Schritt - Hinzufügen von JavaScript:
var subjectObject = { "Front-end": { "HTML": ["Links", "Images", "Tables", "Lists"], "CSS": ["Borders", "Margins", "Backgrounds", "Float"] "JavaScript": ["Variables", "Operators", "Functions", "Conditions"] }, "Back-end": { "PHP": ["Variables", "Strings", "Arrays"], "SQL": ["SELECT", "UPDATE", "DELETE"] } } window.onload = function() { var subjectSel = document.getElementById("subject"); var topicSel = document.getElementById("topic"); var chapterSel = document.getElementById("chapter"); for (var x in subjectObject) { subjectSel.options[subjectSel.options.length] = new Option(x, x); } subjectSel.onchange = function() { // 清空“章节”和“主题”下拉菜单 chapterSel.length = 1; topicSel.length = 1; // 显示正确值 for (var y in subjectObject[this.value]) { topicSel.options[topicSel.options.length] = new Option(y, y); } } topicSel.onchange = function() { // 清空“章节”下拉菜单 chapterSel.length = 1; // 显示正确值 var z = subjectObject[subjectSel.value][this.value]; for (var i = 0; i < z.length; i++) { chapterSel.options[chapterSel.options.length] = new Option(z[i], z[i]); } } }
相关页面
教程:CSS 下拉菜单
教程:如何创建可悬停的下拉菜单
- 上一页 单击下拉菜单
- 下一页 顶部导航栏中的下拉菜单