简体   繁体   English

根据下拉菜单中选择的选项创建另一个下拉菜单

[英]Creating another drop down based on option chosen in a drop down menu

I have a drop down being created, it starts with one that has three options "Choose Gender" "Male?", "Female". 我正在创建一个下拉菜单,它从一个具有三个选项的窗口开始:“选择性别”,“男?”,“女”。 What I want to know is how to make it so that when male is selected, a new drop down menu is created with the options of "Choose your race!", "Human", "Dwarf", "Orc". 我想知道的是如何做到这一点,以便在选择雄性后,将创建一个新的下拉菜单,其中包含“选择种族!”,“人类”,“矮人”,“兽人”选项。 What I am doing now does not seem to work. 我现在所做的似乎不起作用。 I have to use JavaScript (not JQuery) 我必须使用JavaScript(而不是JQuery)

What I have so far: 到目前为止,我有:

var step1 = ["Choose Gender?", "Male", "Female"];
var step2 = [["Choose your race!", "Human", "Dwarf", "Orc"],["Choose your race!", "Elf", "Dwarf"]];
var step3 = [["Human Class", "Warrior", "Sorcerer", "Theif"], ["Elf Class", "Cleric", "Necromancer", "Priest"], ["Dwarf Class", "Cannonner", "Rifelman", "Engineer"], ["Orc Class", "Beserker","Warlock", "Shaman"]];

function init() {
    menuCreate(step1);
    if (selectValue == step1[1]){

        menuCreate(step2);

    }
}
function menuCreate(step2){
    //console.log(step1);
    var myDiv = document.getElementById("mainDiv");
    var titleElement = document.createElement("h1");

    titleElement.setAttribute('style','color:white');
    titleElement.setAttribute('id','guide');

    var selectForm = document.createElement('select');

    selectForm.id = "form";
    myDiv.appendChild(selectForm);



    for (var i=0; i< step2.length; i++){
        var option = document.createElement('option');
        option.value = step2[i];
        option.text = step2[i];
        selectForm.appendChild(option);

    }



} // end menuCreate()

function getSelectValue(){
    var selectValue = document.getElementById("form").value;

}

Any help would be greatly appreciated! 任何帮助将不胜感激! even if it is just a hint to get started! 即使只是开始的提示! I am new to JavaScript. 我是JavaScript新手。 All selects have to be dynamically created in JavaScript. 所有选择都必须使用JavaScript动态创建。 I'm also not allowed to use innerhtml/innertext, the drop downs need to be dynamically created using JS 我也不允许使用innerhtml / innertext,下拉列表需要使用JS动态创建

You can get the body element by tag name, and add to the innerHTML of it to create more HTML elements, as shown in my JSFiddle code: https://jsfiddle.net/qx9cwnuk/ 您可以通过标签名称获取body元素,并将其添加到它的innerHTML中以创建更多HTML元素,如我的JSFiddle代码所示: https ://jsfiddle.net/qx9cwnuk/

function create() {
    var name = document.getElementById('s1').value;
  document.getElementById('d1').innerHTML = "You selected " + name + "<br/><select><option>Option 1</option><option>Option 2</option><option>Option 3</option></select>";
}
<select id="s1">
  <option value="male">Male</option>
  <option value="female">Female</option>
  <option value="orc">Orc</option>
</select>
<button onclick="create()">Select</button><br/>
<div id="d1"></div>

You could do this in a slightly neater way if you use make use of events. 如果您使用事件利用功能,则可以采用稍微更整洁的方式进行。 When you create the options for the "step1" dropdown, attach an event to each of the objects. 当为“ step1”下拉列表创建选项时,将事件附加到每个对象。 There are several ways to do that, my recommendation would be: 有几种方法可以做到这一点,我的建议是:

option.addEventListener("click", <someFunction>);

Where someFunction generates your next level of dropdown. 其中someFunction会生成您的下一个下拉列表级别。 Now you probably want to pass along some information to that next function so you could use an anonymous function like this: 现在您可能想将一些信息传递给该下一个函数,以便可以使用这样的匿名函数:

option.addEventListener("click", () => {
    someFunction(option.value, otherUsefulInformation);
});

Excuse the fancy notation, think of it as a way to execute a statement inside of "addEventListener" instead of giving it a function directly. 请原谅这种花哨的符号,将其视为在“ addEventListener”内部执行语句的一种方式,而不是直接为其赋予功能。 Now you could nest this as deeply as you like and pass along all relevant information. 现在,您可以将其尽可能深地嵌套,并传递所有相关信息。 The last level of this nesting could execute some action based on the information passed along to it. 此嵌套的最后一级可以根据传递给它的信息执行某些操作。

https://www.w3schools.com/js/js_htmldom_eventlistener.asp https://www.w3schools.com/js/js_htmldom_eventlistener.asp

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Functions/Arrow_functions

Code example: 代码示例:

generateLevel1(optionsArray){
    for(<each element in optionsArray){
        let option = <new dom element>
        <do customization and stuff to option>
        option.addEventListener("click", () => {
            // the following statements will be executed when this option is clicked
            if(option == male)
                generateLevel2(nextOptionsArray);
            if(option == female)
                generateLevel2(otherNextOptionsArray);
        }
    }
}

Using a bunch of if statements is maybe not the best way to do this but if you have a small number of options it will get the job done. 使用一堆if语句可能不是执行此操作的最佳方法,但是如果选择的选项较少,则可以完成工作。 Also you might have noticed me using "let" instead of "var". 另外,您可能已经注意到我使用“ let”而不是“ var”。 This may or may not be important in this type of nesting, I haven't written js in a long time so I don't know. 这在这种嵌套中可能重要,也可能不重要,我已经很长时间没有写js了,所以我不知道。 I know I'm not being super descriptive but you only asked for a hint. 我知道我不是超级描述者,但您只要求提供提示。

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

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