<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->0<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.0Transitional//EN">
1<HTML>
2<HEAD>
3 <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
4<TITLE>NewDocument</TITLE>
5<METANAME="Generator"CONTENT="EditPlus">
6<METANAME="Author"CONTENT="">
7<METANAME="Keywords"CONTENT="">
8<METANAME="Description"CONTENT="">
9<scriptlanguage="JavaScript"type="text/javascript">
10//定义了城市的二维数组,里面的顺序跟省份的顺序是相同的。通过selectedIndex获得省份的下标值来得到相应的城市数组
11varcity=[
12["北京","天津","上海","重庆"],
13["南京","苏州","南通","常州"],
14["福州","福安","龙岩","南平"],
15["广州","潮阳","潮州","澄海"],
16["兰州","白银","定西","敦煌"]
17];
18
19functiongetCity(){
20//获得省份下拉框的对象
21varsltProvince=document.form1.province;
22//获得城市下拉框的对象
23varsltCity=document.form1.city;
24
25//得到对应省份的城市数组
26varprovinceCity=city[sltProvince.selectedIndex-1];
27
28//清空城市下拉框,仅留提示选项
29sltCity.length=1;
30
31//将城市数组中的值填充到城市下拉框中
32for(vari=0;i<provinceCity.length;i++){
33sltCity[i+1]=newOption(provinceCity[i],provinceCity[i]);
34}
35}
36</script>
37</HEAD>
38
39<BODY>
40<FORMMETHOD=POSTACTION=""name="form1">
41<SELECTNAME="province"onChange="getCity()">
42<OPTIONVALUE="0">请选择所在省份</OPTION>
43<OPTIONVALUE="直辖市">直辖市</OPTION>
44<OPTIONVALUE="江苏省">江苏省</OPTION>
45<OPTIONVALUE="福建省">福建省</OPTION>
46<OPTIONVALUE="广东省">广东省</OPTION>
47<OPTIONVALUE="甘肃省">甘肃省</OPTION>
48</SELECT>
49<SELECTNAME="city">
50<OPTIONVALUE="0">请选择所在城市</OPTION>
51</SELECT>
52</FORM>
53</BODY>
54</HTML>
55
当你的option对象名称和ID前缀是以数字开头时以上写法还可以这样写(因为当option对象前缀为数字时可能会带来一系列的JS脚本错误提示,所以不建议前缀使用带有数字的名称):
0<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.0Transitional//EN">
1<HTML>
2<HEAD>
3 <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
4<TITLE>NewDocument</TITLE>
5<METANAME="Generator"CONTENT="EditPlus">
6<METANAME="Author"CONTENT="">
7<METANAME="Keywords"CONTENT="">
8<METANAME="Description"CONTENT="">
9<scriptlanguage="JavaScript"type="text/javascript">
10//定义了城市的二维数组,里面的顺序跟省份的顺序是相同的。通过selectedIndex获得省份的下标值来得到相应的城市数组
11varcity=[
12["北京","天津","上海","重庆"],
13["南京","苏州","南通","常州"],
14["福州","福安","龙岩","南平"],
15["广州","潮阳","潮州","澄海"],
16["兰州","白银","定西","敦煌"]
17];
varcityID=[
["0101","0102","0103","0104"],
["0201","0202","0203","0204"],
["0301","0302","0303","0304"],
["0401","0402","0403","0404"],
["0501","0502","0503","0504"]
];
18
19functiongetCity(var1,var2){
20//获得省份下拉框的对象
21var sltProvince=document.getElementById(var1);
22//获得城市下拉框的对象
23var sltCity=document.getElementById(var2);
24
25//得到对应省份的城市数组
26varprovinceCity=city[sltProvince.selectedIndex-1];
27 var provinceCity_ID=cityID[sltProvince.selectedIndex - 1];
28//清空城市下拉框,仅留提示选项
29sltCity.length=1;
30
31//将城市数组中的值填充到城市下拉框中
32if(document.getElementById(var1).value != 0){//当选择option的值为0时不执行下面的
for(vari=0;i<provinceCity.length;i++){
//new Option中的两个参数,前面的值表示为显示内容,后面为value的值
33 sltCity[i+1]=newOption(provinceCity[i],provinceCity_ID[i]);
34 }
}
35}
36</script>
37</HEAD>
38
39<BODY>
40<FORMMETHOD=POSTACTION=""name="form1">
41<SELECTid = "1province" NAME="1province" onChange="getCity('1province','1city')">
42<OPTIONVALUE="0">请选择所在省份</OPTION>
43<OPTIONVALUE="直辖市">直辖市</OPTION>
44<OPTIONVALUE="江苏省">江苏省</OPTION>
45<OPTIONVALUE="福建省">福建省</OPTION>
46<OPTIONVALUE="广东省">广东省</OPTION>
47<OPTIONVALUE="甘肃省">甘肃省</OPTION>
48</SELECT>
49<SELECTid ="1city" NAME="1city">
50<OPTIONVALUE="0">请选择所在城市</OPTION>
51</SELECT>
52</FORM>
53</BODY>
54</HTML>
注:getCity('1province','1city')中的'1province','1city'也可以换写成this.id.