实例
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <select name="" id="" onchange="func(this)"> <option value="">--请选择--</option> <option value="0">广东</option> <option value="1">河南</option> <option value="2">广西</option> <option value="3">北京</option> </select>省 <select name="" id="cid"> <option value="">--请选择--</option> </select>市 </body> <script> var cid = document.getElementById('cid'); function func(obj){ // alert(obj); // alert(obj.value); val = obj.value; //根据我们传递过来的value值来分配二维数组 city = []; city[0] = ['广州','深圳','东莞','惠州','佛山','珠海','中山','汕头']; city[1] = ['郑州','商丘','开封','新乡','洛阳']; city[2] = ['南宁','桂林','北海','钦州','柳州']; city[3] = ['朝阳','海淀','东城','西城','昌平','大兴','顺义']; // city[0][0]=['越秀区','天河']; // console.log(city); // console.log(city[val][0]) // console.log(city[val][1]) // console.log(city[val][2]) // console.log(city[val][3]) // console.log(city[val][4]) // console.log(city[val][5]) // console.log(city[val][6]) // console.log(city[val][7]) // var op = new Option('钦州',0); // console.log(op); // cid.add(op); //清空原来内容 cid.length = 1; //循环创建option标签 for(var i = 0;i<city[val].length;i++){ //console.log(city[val][i]); op = new Option(city[val][i],i); //console.log(op); cid.add(op); } } </script> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例