Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
173 views
in Technique[技术] by (71.8m points)

Display info in second drop down box based on selection of the first from array + javascript

I am the webadmin for my fraternity's website but I am not sure how to tackle this problem. I want two drop down boxes in my form. I want that if the user clicks Alpha (chapter) the second drop down box will display Option 1 and Option 2. But if the user selects Beta (chapter) the second drop down box will display Option 3 and Option 4. I am trying to do this purely in JavaScript. How would I go about achieving this?

var chapter = new Array();
chapter[0] = ["Alpha","one","two","Option 1","three","four"];
chapter[1] = ["Alpha","one","two","Option 2","three","four"];
chapter[2] = ["Beta","one","two","Option 3","three","four"];
chapter[3] = ["Beta","one","two","Option 4","three","four"];
chapter[4] = ["Gamma","one","two","Option 5","three","four"];
chapter[5] = ["Gamma","one","two","Option 6","three","four"];
chapter[6] = ["Delta","one","two","Option 7","three","four"];
chapter[7] = ["Delta","one","two","Option 8","three","four"];
chapter[8] = ["Epsilon","one","two","Option 9","three","four"];
chapter[9] = ["Epsilon","one","two","Option 10","three","four"];
<select>
  <option value="">Choose one</option>
  <option value="Alpha">Alpha</option>
  <option value="Beta">Beta</option>
  <option value="Gamma">Gamma</option>
  <option value="Delta">Delta</option>
  <option value="Epsilon">Epsilon</option>
</select>

<select>
</select>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

sorry i took much time . because i couldn't use jquery .

var chapter = new Array();
chapter[0] = ["Alpha","one","two","Option 1","three","four"];
chapter[1] = ["Alpha","one","two","Option 2","three","four"];
chapter[2] = ["Beta","one","two","Option 3","three","four"];
chapter[3] = ["Beta","one","two","Option 4","three","four"];
chapter[4] = ["Gamma","one","two","Option 5","three","four"];
chapter[5] = ["Gamma","one","two","Option 6","three","four"];
chapter[6] = ["Delta","one","two","Option 7","three","four"];
chapter[7] = ["Delta","one","two","Option 8","three","four"];
chapter[8] = ["Epsilon","one","two","Option 9","three","four"];
chapter[9] = ["Epsilon","one","two","Option 10","three","four"];

document.getElementById("sell").addEventListener("change", addActivityItem, false);

function addActivityItem(){
  var sel= document.getElementById("sel");
  sel.innerHTML = '';
  var brr=[];
var temp= document.getElementById("sell").value;
chapter.forEach(function(a){
        if(a[0]==temp)
         brr.push(a[3]);   
})  ;

 
  brr.forEach(function(b){
  var newOption = document.createElement('option');
   sel.appendChild(newOption);
   newOption.text=b;
  });

}
<select id="sell">
  <option value="">Choose one</option>
  <option value="Alpha">Alpha</option>
  <option value="Beta">Beta</option>
  <option value="Gamma">Gamma</option>
  <option value="Delta">Delta</option>
  <option value="Epsilon">Epsilon</option>
</select>

<select id="sel">
</select>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...