简体   繁体   English

使用JavaScript级联下拉菜单

[英]Cascading Dropdowns using Javascript

I am trying to implement a cascading drop down using this . 我正在尝试使用this进行级联下拉。 I'm not really familiar with Javascript but I think I'm on the right track. 我对Javascript并不是很熟悉,但是我认为我走对了。 It should display the array values [0, 1, 2, 3] as opposed to the values ["Name", "Definition", "Owner"] 它应该显示数组值[0, 1, 2, 3] ,而不是值["Name", "Definition", "Owner"]

var typeObject = {
  StoredProcedures: ["Name", "Definition", "Owner"],
  Tables: ["Name", "Definition", "Owner", "Schema"],
  Views: ["Name", "Definition", "Owner"]
}
window.onload = function() {
    var typeSel = document.getElementById("typeSel"),
      fieldSel = document.getElementById("fieldSel")
    for (var type in typeObject) {
      typeSel.options[typeSel.options.length] = new Option(type, type);
    }
    typeSel.onchange = function() {
      fieldSel.length = 1; // remove all options bar first
      if (this.selectedIndex < 1) return; // done   
      for (var field in typeObject[this.value]) {
        fieldSel.options[fieldSel.options.length] = new Option(field, field);
      }
    }
    typeSel.onchange();

级联图片

I made a modification to your script 我对您的脚本进行了修改

 var typeObject = { StoredProcedures: ["Name", "Definition", "Owner"], Tables: ["Name", "Definition", "Owner", "Schema"], Views: ["Name", "Definition", "Owner"] } window.onload = function () { var typeSel = document.getElementById("typeSel"), fieldSel = document.getElementById("fieldSel") for (var type in typeObject) { typeSel.options[typeSel.options.length] = new Option(type, type); } typeSel.onchange = function () { fieldSel.length = 1; // remove all options bar first if (this.selectedIndex < 1) return; // done var ft = typeObject[this.value]; for (var field in typeObject[this.value]) { fieldSel.options[fieldSel.options.length] = new Option(ft[field], field); } } typeSel.onchange(); } 
 <select name="optone" id="typeSel" size="1"> <option value="" selected="selected">Select type</option> </select> <br/> <br/> <select name="opttwo" id="fieldSel" size="1"> <option value="" selected="selected">Select field</option> </select> 

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

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