单选按钮选择可创建动态下拉列表

时间:2014-02-20 03:42:02

标签: javascript

我正在尝试创建一个动态填充的下拉列表,具体取决于在一组单选按钮中进行的选择。我目前有一组名为“适用性”的单选按钮,选项包括:人员,程序,环境,交付能力,技术适航性和运行适航性。根据选择的那个,我希望下拉框中填充一些选项。我目前正在处理的代码是:

var peList = [“Worker Type”, “Injury Type”];
var enList = [“Undefined”];
var prList = ["Facility", "Building", "Plant", "Damage Type"];
var caList = ["To Be Determined"];
var teList = ["Aircraft", "Component"];
var opList = ["Nothing at the moment"];

if (Applicability.rawValue===”People”) {
           DropDownList4.clearItems;
    for (var i = 0; i < peList.length; i++) { 
        DropDownList4.addItem(peList[i]);
}
        } else if (Applicability.rawValue===”Property”) {
           DropDownList4.clearItems;
    for (var i = 0; i < prList.length; i++) { 
        DropDownList4.addItem(prList[i]);
        }
        } else if (Applicability.rawValue===”Environment”) {
           DropDownList4.clearItems;
    for (var i = 0; i < enList.length; i++) { 
        DropDownList4.addItem(enList[i]);
        }
        } else if (Applicability.rawValue===”CapabilityToDeliver”) {
           DropDownList4.clearItems;
    for (var i = 0; i < caList.length; i++) { 
        DropDownList4.addItem(caList[i]);
        }
        } else if (Applicability.rawValue===”TechnicalAirworthiness”) {
           DropDownList4.clearItems;
    for (var i = 0; i < taList.length; i++) { 
        DropDownList4.addItem(taList[i]);
        }
        } else if (Applicability.rawValue===”OperationalAirworthiness”) {
           DropDownList4.clearItems;
    for (var i = 0; i < opList.length; i++) { 
        DropDownList4.addItem(opList[i]);}

你能告诉我可能出错的地方吗?谢谢

1 个答案:

答案 0 :(得分:0)

clearitems是一种方法,应该被称为DropDownList4.clearItems();请注意代码中缺少的括号。 此外,通过使用开关使代码更易于维护。例如:

var a,al,ax; // array, array length, array index
switch(Applicability.rawValue){
case 'People': a=['Worker Type', 'Injury Type'];break;
case 'Property': a=['Facility', 'Building', 'Plant', 'Damage Type'];break;
case 'Environment': a=['Undefined'];break;
case 'CapabilityToDeliver': a=['To Be Determined'];break;
case 'TechnicalAirworthiness': a=['Aircraft', 'Component'];break;
case 'OperationalAirworthiness': a=['Nothing at the moment'];break;
default:
 // put your exception handling code here.
}
DropDownList4.clearItems();
for(ax=0,al=a.length;ax<al;ax++){ 
 DropDownList4.addItem(a[ax]);
}