在外部js,实现从一个select ,控制另外一个select列表。比如select1中选择“湖北省”,就把一些城市添加到select2中去。最好有源码。谢了。

解决方案 »

  1.   

    我的email: [email protected]
    谢谢、
      

  2.   


    <html><head>
    <title>会员搜索</title>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    </head>
    <body bgcolor="#FFFFFF" text="#000000">
    <script language="javascript" src="cities.js"></script>

    <table border="0" cellspacing="0" cellpadding="4" width="90%" align="center">
    <form action="" method=get name=dxform2>
    <tr>
    <td width="30%" align="left">所在省市:</td>
            <td>
    <select name='place' onchange="chgBox(this);">
       <option value="0">河南</option>
       <option value="1">湖南</option>
       <option value="2">江西</option>
    </select>
    <select id="citySel" name="city">
       <option value="-1">不限</option>
       <option value="01">郑州</option>
       <option value="02">南阳</option>
       <option value="03">信阳</option>
    </select>
         </td> </tr>
    </form>
    </table>
            </body>
    </html>
    <script>
    //城市
    var heNanArr = new Array(new item("郑州","01"),new item("南阳","02"),new item("信阳","03"));//河南
    var huNanArr = new Array(new item("长沙","11"),new item("衡阳","12"),new item("岳阳","13"));//湖南
    var jiangXiArr = new Array(new item("南昌","21"),new item("九江","22")); //江西
    //省份数组,每个元素对应一个省份的城市数组,
    //保证省份控件中的option顺序跟这个数组一致,对应value值是下标
    var provinceArr = new Array(heNanArr,huNanArr,jiangXiArr); 
    //===============建立一个类 ,表示一个option信息=,两个属性===================
    function item(label, val) {
    this.label  = label;
    this.val  = val;
    }
    //============实现级连=====================
    function chgBox(resObj){
       var destObj = document.getElementById("citySel");//目标select控件
       var cityArr = provinceArr[resObj.selectedIndex];//当前省份下的城市数组
       //目标select控件中删除原来的option(保留第一个)
       for(var i = 1; i < destObj.options.length ; i++){
         destObj.options[i] = null;;
       }
       //写入当前省份的城市
       for(var j = 0; j < cityArr.length ; j++){
         var optionObj = new Option(cityArr[j].label,cityArr[j].val);
         destObj.options[j+1] = optionObj;
       }
    }
    </script>
      

  3.   

    初做网站,谢谢大家的指导,还有个问题,就是在外部的JS里面,为什么doucment的点后面没有任何东西啊?