动态下拉列表混合选项

时间:2016-03-18 23:46:31

标签: javascript html

以下用于创建动态下拉列表的javascript函数似乎在选择了另一个列表时保留了上一个列表中的选项,并且与上一个列表不同。

function dynamicdropdown(listindex)
{
    switch (listindex)
    {
    case "assaultrifle" :
        document.getElementById("weaponbody").options[0]=new Option("Select Assault Rifle Body","");
        document.getElementById("weaponbody").options[1]=new Option("ACR","acr");
        document.getElementById("weaponbody").options[2]=new Option("ACR-E","acre");
        document.getElementById("weaponbody").options[3]=new Option("AK-47","ak47");
        document.getElementById("weaponbody").options[4]=new Option("AK-74","ak74");
        document.getElementById("weaponbody").options[5]=new Option("Classic FAL","classicFal");
        document.getElementById("weaponbody").options[6]=new Option("LWRC M6","lwrcm6");
        document.getElementById("weaponbody").options[7]=new Option("Military M416","militaryM416");
        document.getElementById("weaponbody").options[8]=new Option("M4","m4");
        document.getElementById("weaponbody").options[9]=new Option("P416","p416");
        document.getElementById("weaponbody").options[10]=new Option("SCAR-L","scarl");
        document.getElementById("weaponbody").options[11]=new Option("Steyr Aug","steyrAug");
        document.getElementById("weaponbody").options[12]=new Option("LVOA-S","lvoas");
        document.getElementById("weaponbody").options[13]=new Option("SA-58","sa58");
        break;
    case "lmg" :
        document.getElementById("weaponbody").options[0]=new Option("Select LMG Body","");
        document.getElementById("weaponbody").options[1]=new Option("L86 LSW","l86lsw");
        document.getElementById("weaponbody").options[2]=new Option("M249 B","m249b");
        document.getElementById("weaponbody").options[3]=new Option("M249 Tactical Para","m249TacticalPara");
        document.getElementById("weaponbody").options[4]=new Option("M60","m60");
        document.getElementById("weaponbody").options[5]=new Option("M60 E4","m60e4");
        document.getElementById("weaponbody").options[6]=new Option("M60 E6","m60e6");
        document.getElementById("weaponbody").options[7]=new Option("MK46","mk46");
        document.getElementById("weaponbody").options[8]=new Option("RPK-74","rpk74");
        break;
    case "pistol" :
        document.getElementById("weaponbody").options[0]=new Option("Select Pistol Body","");
        document.getElementById("weaponbody").options[1]=new Option("M1911","m1911");
        document.getElementById("weaponbody").options[2]=new Option("M9","m9");
        document.getElementById("weaponbody").options[3]=new Option("PF45","pf45");
        document.getElementById("weaponbody").options[4]=new Option("586 Magnum","586Magnum");
        document.getElementById("weaponbody").options[5]=new Option("Px4 Storm","px4Storm");
        document.getElementById("weaponbody").options[6]=new Option("M9 A1","m9a1");
        document.getElementById("weaponbody").options[7]=new Option("Defiler","defiler");
        document.getElementById("weaponbody").options[8]=new Option("M45A1","m45a1");
        break;
    case "shotgun" :
        document.getElementById("weaponbody").options[0]=new Option("Select Shotgun Body","");
        document.getElementById("weaponbody").options[1]=new Option("M1014","m1014");
        document.getElementById("weaponbody").options[2]=new Option("Super 90","super90");
        document.getElementById("weaponbody").options[3]=new Option("M870","shipped");
        document.getElementById("weaponbody").options[4]=new Option("SASG-12","sasg12");
        document.getElementById("weaponbody").options[5]=new Option("Double Barrel Shotgun","doubleBarrelShotgun");
        document.getElementById("weaponbody").options[6]=new Option("Saiga 12","saiga 12");
        break;
    case "smg" :
        document.getElementById("weaponbody").options[0]=new Option("Select SMG Body","");
        document.getElementById("weaponbody").options[1]=new Option("MP5","mp5");
        document.getElementById("weaponbody").options[2]=new Option("MP5-A4 Burst Fire","mpa4BurstFire");
        document.getElementById("weaponbody").options[3]=new Option("T821","t821");
        document.getElementById("weaponbody").options[4]=new Option("SMG-9","smg9");
        document.getElementById("weaponbody").options[5]=new Option("Vector","vector");
        document.getElementById("weaponbody").options[6]=new Option("Black Market T821","blackMarkett821");
        document.getElementById("weaponbody").options[7]=new Option("AUG A3 Para XS","AUG A3 Para XS");
        break;
    case "dmr" :
        document.getElementById("weaponbody").options[0]=new Option("Select DMR Body","");
        document.getElementById("weaponbody").options[1]=new Option("M1A","m1a");
        document.getElementById("weaponbody").options[2]=new Option("SRS","srs");
        document.getElementById("weaponbody").options[3]=new Option("SOCOM 16","socom16");
        document.getElementById("weaponbody").options[4]=new Option("Mk17 SR","mk17sr");
        document.getElementById("weaponbody").options[5]=new Option("SCAR-H","scarh");
        document.getElementById("weaponbody").options[6]=new Option("M44","m44");
        break;
    }
    return true;
}

html在这里:

<div class="sub_category_div" id="sub_category_div">
<label class="input-label">WEAPON BODY</label>
    <script type="text/javascript" language="JavaScript">
    document.write('<select name="weaponbody" id="weaponbody"><option value="">Weapon Body</option></select>')
    </script>
</div>

与突击步枪列表混合的SMG列表示例如下图所示:

Mixed Option List

关于为什么会这样做的任何想法都会有所帮助。

2 个答案:

答案 0 :(得分:1)

在添加新选项之前清除选项:

var first = document.getElementsByTagName('li')[0];
var label = document.getElementsByTagName('label')[0];

first.onmouseover = function(){
   label.style.display = 'block';
}

first.onmouseout = function(){
   label.style.display = 'none';
}

答案 1 :(得分:1)

我刚补充说:

document.getElementById('weaponbody').options.length = 0;

到这里的onchange:

<select id="weapontype" name="weapontype" onchange="javascript:  dynamicdropdown(this.options[this.selectedIndex].value);">

结果:

<select id="weapontype" name="weapontype" onchange="javascript: document.getElementById('weaponbody').options.length = 0; dynamicdropdown(this.options[this.selectedIndex].value);">

完美无缺。