<script language="JavaScript" type="text/javascript">
var city=[
["city1","city2","city3","city4"],
["city5","city6","city7"],
["city8","city9","city10"],
];
function getCity(){
var sltProvince=document.form1.province;
var sltCity=document.form1.city;
var provinceCity=city[sltProvince.selectedIndex - 1];
sltCity.length=1;
for(var i=0;i<provinceCity.length;i++){
sltCity[i+1]=new Option(provinceCity[i],provinceCity[i]);
}
}
</script>
<FORM METHOD=POST ACTION="" name="form1">
<SELECT NAME="province" onChange="getCity()">
<OPTION VALUE="0">select province </OPTION>
<OPTION VALUE="province1">province 1 </OPTION>
<OPTION VALUE="province2">province2</OPTION>
<OPTION VALUE="province3">province3 </OPTION>
</SELECT>
<SELECT NAME="city">
<OPTION VALUE="0">select the city</OPTION>
</SELECT>
</FORM>
以上代码是根据省选择其城市。有一些我不太懂的线。希望有人可以解释一下。谢谢。
1,这些线路的作用和含义是什么?
var provinceCity=city[sltProvince.selectedIndex - 1];
和
sltCity.length=1;
sltCity[i+1]=new Option(provinceCity[i],provinceCity[i]);
答案 0 :(得分:3)
根据所选省份更新城市列表。
// Get list of cities in the province, based on the index of the selected province.
// -1 is because the first entry in the list of city lists is 0, but in the province
// options the first one has index 1.
var provinceCity=city[sltProvince.selectedIndex - 1];
// Remove all options except the first one that says "select city"
sltCity.length=1;
// Add all the cities for this province to <SELECT NAME="province">
for(var i=0;i<provinceCity.length;i++){
// add an option to the with value and contents set to provinceCity[1]:
// HTML: <OPTION VALUE="provinceCity[1]">provinceCity[1]</OPTION>
sltCity[i+1]=new Option(provinceCity[i],provinceCity[i]);
}
答案 1 :(得分:1)
1,这些线的作用和意义是什么?
var provinceCity=city[sltProvince.selectedIndex - 1];
- 这意味着省级城市被创建为变量,其中包含当时选择的ComboBox的值。
sltCity.length=1;
....
sltCity[i+1]=new Option(provinceCity[i],provinceCity[i]);
选择省后,脚本将加载该省的城市。选项意味着您要为界面创建新的下拉选项。
答案 2 :(得分:1)
基本上,这是根据省份首次下拉菜单中的选择,将城市添加到城市下拉菜单中。 var provinceCity=city[sltProvince.selectedIndex - 1];
本质上是抓住city
数组中位于数组索引处的城市列表,该数组比所选省份的索引小1。
答案 3 :(得分:1)
首先要了解的是,city是一个数组数组。因此,
var provinceCity=city[sltProvince.selectedIndex - 1];
在sltProvince的选定索引(所选项目的索引)减去1时,将provinceCity设置为city的索引值。因此,如果所选索引为3,则provinceCity将等于city [2]或
["city5","city6","city7"]
这最终会影响for循环的长度。
sltCity.length=1;
将sltCity的长度设置为1。
sltCity[i+1]=new Option(provinceCity[i],provinceCity[i]);
将city数组的值设置为i(从for循环)加1到一个新的Option实例,这是javascript创建选项标记的方式:
<OPTION> </OPTION>