如何在Javascript依赖选项中添加更多选项?

问题描述:

我想在这个文件中添加更多的下拉菜单中选择:如何在Javascript依赖选项中添加更多选项?

http://parseven.com/java_ex.html

现在它有两个,我想提出三点多。第三个选项必须依赖于第二个选项等等。 请告诉我。提前致谢。

数据来自哪里?我不确定你最需要哪个部分的答案:

  1. 您需要将事件处理程序添加到第一个下拉列表的OnChange事件中。
  2. 在事件处理程序中,您需要从某处获取数据(AJAX?)
  3. 然后您需要获取新数据和add it to the second dropdown

这里基本上是你做什么:

document.getElementById('dropdown1').onchange = yourFunction; 

function yourFunction() { 
    //Here is where you need to get the data 

    //Then you need to add them to the other dropdown like this: 
    var dropdown2 = document.getElementById('dropdown2'); 

    var optn = document.createElement("OPTION"); 
    optn.text = text; 
    optn.value = value; 
    dropdown2.options.add(optn); 
}