<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>province.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
<script>
var data =
[
{
name:"湖北",
id:99,
cities:[
{name:"武汉",id:3},
{name:"宜昌",id:9},
{name:"荆州",id:6}]
},
{
name:"湖南",
id:88,
cities:[
{name:"长沙",id:4},
{name:"岳阳",id:7},
{name:"郴州",id:10}]
},
{
name:"四川",
id:77,
cities:[
{name:"成都",id:5},
{name:"宜宾",id:11},
{name:"绵阳",id:8},
{name:"自贡",id:12}]
}
];
function fillProvince()
{
for(var i=0;i<data.length;i++)
{
var provincedata = data[i];
var optProvince = new Option(provincedata.name,provincedata.id);
document.getElementById("provinceId").options.add(optProvince);
}
}
function clearOptions(selElement)
{
var options = selElement.options;
var len = options.length;
/*for(var i=0;i<len;i++)
{
options.remove(0);
}*/
for(var i=len-1;i>=0;i--)
{
selElement.remove(i);
}
}
function fillCity(provinceIndex)
{
//document.getElementById("cityId").options.length=0;
clearOptions(document.getElementById("cityId"));
var citydata = data[provinceIndex].cities;
for(var i=0; i<citydata.length; i++)
{
var optCity = new Option(citydata[i].name,citydata[i].id);
document.getElementById("cityId").options.add(optCity);
}
}
</script>
</head>
<body>
<select id="provinceId" name="provinceId" onChange="fillCity(this.selectedIndex)">
</select>
<select id="cityId" name="cityId">
</select>
<script>
fillProvince();
fillCity(document.getElementById("provinceId").selectedIndex);
</script>
</body>
</html>
===========================================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>province.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
<script>
var data = [["湖北","99","武汉","3","宜昌","9","荆州","6"],
["湖南","88","长沙","4","岳阳","7","郴州","10"],
["四川","77","成都","5","宜宾","11","绵阳","8","自贡","12"]];
function fillProvince()
{
/*讲课时先要用演示
document.getElementById("provinceId")
.options.add(new Option("武汉","3"));
这种静态数据测试,以便基础差的学员更容易理解
*/
//todo here!!!!!
for(var i=0; i<data.length; i++){
var provincedata = data[i];
document.getElementById("provinceId").options.add(new Option(provincedata[0],provincedata[1]));
}
}
function clearOptions(selElement)
{
var options = selElement.options;
var len = options.length;
/*for(var i=0;i<len;i++)
{
options.remove(0);
}*/
for(var i=len-1;i>=0;i--)
{
//options.remove(i);在火狐下不行
selElement.remove(i);
}
}
function fillCity(provinceIndex)
{
//document.getElementById("cityId").options.length=0;
clearOptions(document.getElementById("cityId"));
//todo here!!!!!
var citydata = data[provinceIndex];
for(var j=1; j<citydata.length/2; j++){
document.getElementById("cityId").options.add(new Option(citydata[j*2],citydata[j*2+1]));
}
}
</script>
</head>
<body>
<select id="provinceId" name="provinceId" onChange="fillCity(this.selectedIndex)">
</select>
<select id="cityId" name="cityId">
</select>
<script>
fillProvince();
fillCity(document.getElementById("provinceId").selectedIndex);
</script>
</body>
</html>
====================================================