[英]Dynamic drop down menu will not create based on selection
So i'm working on a project that creates three drop down menus that appear based on the option selected on the first. 因此,我正在一个项目中创建一个基于第一个选择的选项而出现的三个下拉菜单。 When I select from the first menu, "Male" or "Female" the correct menus appear( example, when Male is selected, the drop down with the options ""Human", "Dwarf", "Orc" appear). However, after that I can not get a third drop down menu to appear based on the next selection using the same methods before. At this point I am very lost.
当我从第一个菜单“男性”或“女性”中选择时,会出现正确的菜单(例如,当选择“男性”时,会出现带有“人”,“矮人”,“兽人”选项的下拉菜单。)之后,我无法使用以前相同的方法根据下一个选择显示第三个下拉菜单,这时我很失落。
var step1 = ["Choose Gender?", "Male", "Female"];
var step2 = [["Choose your race!", "Human", "Dwarf", "Orc"],["Choose your race!", "Fairy", "Elf", "Centaur"]];
var step3 = [["Human Class!", "Warrior", "Sorcerer", "Theif"], ["Elf Class!", "Cleric", "Necromancer", "Priest"], ["Dwarf Class!", "Cannonner", "Rifelman", "Engineer"], ["Orc Class!", "Beserker","Warlock", "Shaman"], ["Fairy Class!", "Druid", "Arcanist", "Mystic"]];
function testData(){
menuCreate(step3[0]);
}
function init() {
menuCreate(step1);
var dropdown = document.getElementById("form");
dropdown.onchange = function(event){
if (dropdown.value == "Male"){
//menuCreate(step2[0]);
var myDiv = document.getElementById("mainDiv");
var next = document.createElement('input');
next.setAttribute('type','button');
next.setAttribute('value','Next');
next.setAttribute('onclick','maleRace()');
myDiv.appendChild(next);
} else if (dropdown.value == "Female"){
//menuCreate(step2[1]);
myDiv = document.getElementById("mainDiv");
femaleNext = document.createElement('input');
femaleNext.setAttribute('type','button');
femaleNext.setAttribute('value','Next');
femaleNext.setAttribute('onclick','femaleRace()');
myDiv.appendChild(femaleNext);
}
}
} // end init()
function menuCreate(step1){
//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< step1.length; i++){
var option = document.createElement('option');
option.value = step1[i];
option.text = step1[i];
selectForm.appendChild(option);
}
} // end menuCreate()
function maleRace(){
menuCreate(step2[0]);
var down = document.getElementById("form");
down.onchange = function(event){
if (down.value == "Human"){
menuCreate(step3[0]);
var div = document.createElement("div");
var next = document.createElement('input');
next.setAttribute('type','button');
next.setAttribute('value','Next');
next.setAttribute('onclick','maleRace()');
div.appendChild(next);
}
}
} // end maleRace()
Please note I cant use JQUERY or innerHTML/innerText. 请注意,我不能使用JQUERY或innerHTML / innerText。 I have to do this using normal JavaScript the problem is that when I select "Human" on the second menu, I am supposed to get another menu that uses step3[0].
我必须使用普通的JavaScript来执行此操作,问题是当我在第二个菜单上选择“人类”时,应该获得另一个使用step3 [0]的菜单。
In the menuCreate
function, you're setting an id
attribute to the select
element: selectForm.id = "form";
在
menuCreate
函数中,您要为select
元素设置id
属性: selectForm.id = "form";
. 。 After you call it for the second time (for the second step), you have two elements with the same id in your document.
第二次调用(第二步)之后,文档中有两个具有相同ID的元素。 Then, when you try to add the
onchange
event handler on the maleRace
function - document.getElementById("form"); down.onchange = function(event){
然后,当您尝试在
maleRace
函数上添加onchange
事件处理程序时maleRace
document.getElementById("form"); down.onchange = function(event){
document.getElementById("form"); down.onchange = function(event){
you actually attach this handler to the first select
element (the gender one) instead of the second one. document.getElementById("form"); down.onchange = function(event){
您实际上将此处理程序附加到第一个select
元素(性别),而不是第二个。
A nice way to solve this problem, will be to add a second argument to the menuCreate
function like so: function menuCreate(step1, id){
. 解决此问题的一种好方法是在
menuCreate
函数中添加第二个参数,例如: function menuCreate(step1, id){
。 Then, change this line a bit from selectForm.id = "form";
然后,从
selectForm.id = "form";
稍微更改此行selectForm.id = "form";
to selectForm.id = id;
选择
selectForm.id = id;
. 。
Then, change every call to the createMenu
function to have its own id: 然后,将对
createMenu
函数的每个调用更改为具有自己的ID:
function init() {
menuCreate(step1, 'step1form');
var dropdown = document.getElementById("step1form");
function maleRace(){
menuCreate(step2[0], 'step2form');
var down = document.getElementById("step2form");
You can check out this codepen: https://codepen.io/anon/pen/xXJwBv?editors=1011 您可以检出此Codepen: https ://codepen.io/anon/pen/xXJwBv ? editors = 1011
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.