<body><section> <a>省份</a> <select id="province"> <option value="0">--请选择--</option> <option value="1">山东</option> //通过不同的value来区分不同的省份 <option value="2">山西</option> </select> <a>城市</a> <select id="city"> <option>--请选择--</option> </select></section></body><script> var arr = new Array(); arr[1] = ["莱芜","济南","东营"]; arr[2] = ["长治","太原","壶关"]; var provinceInput = document.getElementById("province"); //获取省份 var cityInput = document.getElementById("city"); //获取市 provinceInput.onchange = function () { cityInput.options.length = 1;//当省改变的时候让市的长度为1 for(var i = 0;i < arr[provinceInput.value].length;i++) { cityInput.add(new Option(arr[provinceInput.value][i])); //向市里面添加对应的数组元素 } }</script>
时间: 2024-11-04 21:59:27