简体   繁体   English

在级联下拉列表变量中声明数组 javascript

[英]Declaring array within variable for cascading dropdown javascript

Im a beginner and Im having trouble figuring out on how to make this work.我是初学者,我不知道如何完成这项工作。 I'm not sure if I'm even saying this right, but I have to declare an array within another variable so that when a person clicks on a value in one dropdown, values relative to that show up on the next one dropdown.我不确定我说的是否正确,但我必须在另一个变量中声明一个数组,这样当一个人单击一个下拉列表中的值时,与该值相关的值会显示在下一个下拉列表中。 Right now, the values don't even show up on the dropdown.现在,这些值甚至不会显示在下拉列表中。 Any help would be appreciated.任何帮助,将不胜感激。

 var subjectObject = { "Serif": [ "Times New Roman", "Georgia", "Garamound" ], "Sans-serif": [ "Arial", "Verdana", "Helvetica" ], "Monospace": [ "Courier New", "Lucida Console", "Monaco" ], "Cursive": [ "Brush Script MT", "Lucida Handwriting" ], "Fantasy": [ "Copperplate", "Papyrus" ] }; window.onload = function() { var subjectSel = document.getElementById("subject"); var topicSel = document.getElementById("topic"); for (var x in subjectObject) { subjectSel.options[subjectSel.options.length] = new Option(x, x); } subjectSel.onchange = function() {    //empty Chapters- and Topics- dropdowns     topicSel.length = 1; //display correct values for (var y of subjectObject[this.value]) { topicSel.options[topicSel.options.length] = new Option(y, y); } } topicSel.onchange = function() { var changeFontStyle = function(font) { document.getElementById( "demo").style.fontFamily = font.value;  } } }
 <p id="demo"> HEIIDXCD </p> <form name="form1" id="form1" action="/action_page.php"> Font Family: <select name="subject" id="subject"> <option value="" selected="selected">Select Font Family</option> </select> <br><br> Font Names: <select name="topic" id="topic" onchange="changeFontStyle (this);"> <option value="" selected="selected">Please select font family first</option> </select> <br><br> </form>

A way to do that...一种方法来做到这一点...
( forms and forms elements doesn't need to use ID attributes) ( forms 和 forms 元素不需要使用 ID 属性)

 const pDemo = document.querySelector('p#demo'), formOne = document.forms.form1, subjectObject = { 'Serif': [ 'Times New Roman', 'Georgia', 'Garamound' ], 'Sans-serif': [ 'Arial', 'Verdana', 'Helvetica' ], 'Monospace': [ 'Courier New', 'Lucida Console', 'Monaco' ], 'Cursive': [ 'Brush Script MT', 'Lucida Handwriting' ], 'Fantasy': [ 'Copperplate', 'Papyrus' ] }; for (let font in subjectObject) { formOne.subject.add( new Option(font,font)) } formOne.subject.onchange =_=> { formOne.topic.length = 1 formOne.topic.value = '' pDemo.textContent = ' HEIIDXCD ' for( let fontName of subjectObject[formOne.subject.value]) { formOne.topic.add( new Option(fontName,fontName)) } } formOne.topic.onchange =_=> { pDemo.textContent = formOne.subject.value + ' / ' + formOne.topic.value pDemo.style.fontFamily = formOne.topic.value; }
 <p id="demo"> HEIIDXCD </p> <form name="form1" action="/action_page.php"> Font Family: <select name="subject" > <option value="" selected disabled>Select Font Family</option> </select> <br><br> Font Names: <select name="topic" > <option value="" selected disabled>Please select font family first</option> </select> <br><br> </form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM