两个二级联动菜单的js代码

发布时间:2019-11-09编辑:脚本学堂
js代码实现的二个二级联动菜单,简单实用,是学习编写js菜单的好例子,有需要的朋友,不要错过了。

js代码1:
 

复制代码 代码示例:
<script language = "javascript">
var onecount;
subcat = new Array();
subcat[0] = new Array("奥迪A4","奥迪","奥迪A4");        
subcat[1] = new Array("奥迪A6","奥迪","奥迪A6");        
subcat[2] = new Array("捷达","大众(一汽)","捷达");        
subcat[3] = new Array("宝来","大众(一汽)","宝来");        
onecount=3;
function changelocation(locationid)
    {
    document.form1.select.length = 1; 
    var locationid=locationid;
    var i;
    for (i=0;i < onecount; i++)
        {
            if (subcat[i][1] == locationid)
            { 
                document.form1.select.options[document.form1.select.length] = new Option(subcat[i][0],subcat[i][2]);
            }        
        }
    }
</script>
<form name=form1>
<select name="select1" onChange="changelocation(document.form1.select1.options[document.form1.select1.selectedIndex].value)" size="1">
<option value=奥迪>奥迪</option>
<option value=现代>现代</option>
<option value=菲亚特系列>菲亚特系列</option>
</select>
<select name="select">
<option value="" selected>未指定小类</option>
</select>
</form>

js代码2:
 

复制代码 代码示例:
<form name="menu_form" action="#" method="post">
<p>
<select name="example" size="1" onChange="redirect(this.options.selectedIndex)">
<option>脚本学堂测试---1</option>
<option>脚本学堂测试---2</option>
<option>脚本学堂测试---3</option>
</select>
<select name="stage2" size="1">
<option value="#">Website Abstraction</option>
<option value="#">Dynamicdrive.com</option>
<option value="#">javascript</option>
</select>
<input type="button" name="test" value="Go!"
onClick="go()">
</p>
<script>
<!--
var groups=document.doublecombo.example.options.length
var group=new Array(groups)
for (i=0; i<groups; i++)
group[i]=new Array()
group[0][0]=new Option("Website Abstraction","#")
group[0][1]=new Option("Dynamicdrive","#")
group[0][2]=new Option("javascript","#")
group[1][0]=new Option("123123","#")
group[1][1]=new Option("javascript2000","#")
group[1][2]=new Option("脚本学堂","#")
group[2][0]=new Option("Hotbot","#")
group[2][1]=new Option("Infoseek","#")
group[2][2]=new Option("Excite","#")
group[2][3]=new Option("Lycos","#")
var temp=document.doublecombo.stage2
function redirect(x){
for (m=temp.options.length-1;m>0;m--)
temp.options[m]=null
for (i=0;i<group[x].length;i++){
temp.options[i]=new Option(group[x][i].text,group[x][i].value)
}
temp.options[0].selected=true
}
function go(){
location=temp.options[temp.selectedIndex].value
}
//-->
</script>
</form>

以上代码演示效果图:
二级联动菜单