//////////////////////////////////////////////////////////////////定义城市!注意我的数组变量写法!
var a1=new Array("温州","平阳","乐清","苍南","文成","泰顺");//浙江选项
var a2=new Array("赤壁","孝感","襄樊");//湖北选项
var a3=new Array("岳阳","长沙","郴州","怀化");//湖南选项...,Bullcn'Blog - 分享、交流、进步。" /> 用JavaScript制作关联下拉选择框 - Bullcn'Blog

用JavaScript制作关联下拉选择框


<script language="JavaScript">
//////////////////////////////////////////////////////////////////定义城市!注意我的数组变量写法!
var a1=new Array("温州","平阳","乐清","苍南","文成","泰顺");//浙江选项
var a2=new Array("赤壁","孝感","襄樊");//湖北选项
var a3=new Array("岳阳","长沙","郴州","怀化");//湖南选项
var a4=new Array("西安","安康","渭南");//陕西选项
var a5=new Array("洛阳","郑州","驻马店");//河南选择
var a6=new Array("合肥","铜陵","黄山");//安徽选项
//////////////////////////////////////////////////
function changes()//定义联动函数!
{
///////////////////////////////////////////////联动开始!  
   for(s=1;s<document.all("sel").length;s++)
   {  //option不超过所有省份的总和!
     if(document.all("sel").options[s].selected)//假如第 n  个option选项被选中!
        {
             for(k=document.all("se2").length;k>-1;k--)//获取目标下拉的option总数!从大到小!
            {
              document.all("se2").options.remove(k);//清空所有的目标下拉的option!
              }


         for(m=0;m<eval("a"+s).length;m++)//注入城市数组,eval("a"+s)表示检测括号内的语法,将其视为正确!
             {
                var news=document.createElement("option")//为页面创建一个option对象!
                    news.text=eval("a"+s)[m];//目前选项的文本值!
                 document.all("se2").options.add(news); //将数组注入目标下拉
               }
    }
   }  
  
}
////////////////////////////////////////////////联动完成,简单吧!哈哈哈!
</script>
<body>

<form>
        <select name="sel" id="sel" onChange="changes()" >
          <option value="浙江">浙江</option>
          <option value="湖北">湖北</option>
          <option value="湖南">湖南</option>
          <option value="陕西">陕西</option>
          <option value="河南">河南</option>
          <option value="安徽">安徽</option>
        </select>
        <select name="select4" id="se2"  width="120" >
        </select>
</form>


脚本说明:
把如下代码加入<body>区域中
<!-- 二级联动 Start -->

<script language="JavaScript">
<!--

var subcat = new Array();
subcat[0] = new Array('10','1','=1')
subcat[1] = new Array('10','2','=2')
subcat[2] = new Array('10','3','=3')
subcat[3] = new Array('10','4','=4')
subcat[4] = new Array('10','5','=5')
subcat[5] = new Array('10','6','=6')
subcat[6] = new Array('10','7','=7')
subcat[7] = new Array('10','8','=8')
subcat[8] = new Array('10','9','=9')
subcat[9] = new Array('10','10','=10')
subcat[10] = new Array('20','11','=11')
subcat[11] = new Array('20','12','=12')
subcat[12] = new Array('20','13','=13')
subcat[13] = new Array('20','14','=14')
subcat[14] = new Array('20','15','=15')
subcat[15] = new Array('20','16','=16')
subcat[16] = new Array('20','17','=17')
subcat[17] = new Array('20','18','=18')
subcat[18] = new Array('20','19','=19')
subcat[19] = new Array('20','20','=20')

function changeselect1(locationid)
{
    document.form1.s2.length = 0;
    document.form1.s2.options[0] = new Option('==请选择==','');
    for (i=0; i<subcat.length; i++)
    {
        if (subcat[i][0] == locationid)
        {document.form1.s2.options[document.form1.s2.length] = new Option(subcat[i][1], subcat[i][2]);}
    }
}
//-->
</script>

<form name="form1">

二级联动:

<select name="s1" onChange="changeselect1(this.value)">
<option>==请选择==</option>
<option value="10">1-10</option>
<option value="20">11-20</option>
</select>

<select name="s2" onChange="alert(this.value)">
<option>==请选择==</option>
</select>

</form>

<!-- 二级联动 Over -->

<!-- 三级联动 Start -->

<script language="JavaScript">
<!--

var subval = new Array();
subval[0] = new Array('10','单数','1','=1')
subval[1] = new Array('10','双数','2','=2')
subval[2] = new Array('10','单数','3','=3')
subval[3] = new Array('10','双数','4','=4')
subval[4] = new Array('10','单数','5','=5')
subval[5] = new Array('10','双数','6','=6')
subval[6] = new Array('10','单数','7','=7')
subval[7] = new Array('10','双数','8','=8')
subval[8] = new Array('10','单数','9','=9')
subval[9] = new Array('10','双数','10','=10')
subval[10] = new Array('20','单数','11','=11')
subval[11] = new Array('20','双数','12','=12')
subval[12] = new Array('20','单数','13','=13')
subval[13] = new Array('20','双数','14','=14')
subval[14] = new Array('20','单数','15','=15')
subval[15] = new Array('20','双数','16','=16')
subval[16] = new Array('20','单数','17','=17')
subval[17] = new Array('20','双数','18','=18')
subval[18] = new Array('20','单数','19','=19')
subval[19] = new Array('20','双数','20','=20')

function changeselect2()
{
    document.form2.s2.length = 0;
    document.form2.s2.options[0] = new Option('==请选择==','');
    document.form2.s2.options[1] = new Option('选择单数','单数');
    document.form2.s2.options[2] = new Option('选择双数','双数');
    document.form2.s3.length = 0;
    document.form2.s3.options[0] = new Option('==请选择==','');
}

function changeselect3(sub1,sub2)
{
    document.form2.s3.length = 0;
    document.form2.s3.options[0] = new Option('==请选择==','');
    for (i=0; i<subval.length; i++)
    {
        if ((subval[i][0] == sub1) & (subval[i][1] == sub2))
        {document.form2.s3.options[document.form2.s3.length] = new Option(subval[i][2], subval[i][3]);}
    }
}
//-->
</script>

<form name="form2">

三级联动:

<select name="s1" onChange="changeselect2()">
<option>==请选择==</option>
<option value="10">1-10</option>
<option value="20">11-20</option>
</select>

<select name="s2" onChange="changeselect3(document.form2.s1.value,this.value)">
<option>==请选择==</option>
</select>

<select name="s3" onChange="alert(this.value)">
<option>==请选择==</option>
</select>

</form>

<!-- 三级联动 Over -->



文章来自: Original
引用通告: 查看所有引用 | 我要引用此文章
Tags:
相关日志:
评论: 7 | 引用: 0 | 查看次数: 876
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 20 字 | UBB代码 关闭 | [img]标签 关闭