JavaScript实现省市县级联
/*functionLoadProvince(objList){objList.length=provinceid.length;for(vari=0;i<provinc...
/*
function LoadProvince(objList)
{
objList.length=provinceid.length;
for (var i=0;i<provinceid.length;i++)
{
objList.options[i].text=provincename[i];
objList.options[i].value=provinceid[i];
}
}
*/
var provinceid = new Array;
var provincename = new Array;
var cityid = new Array;
var cityname = new Array;
var areaid = new Array;
var areaname = new Array;
cityid[0]="0101";
cityname[0]="东城区";
cityid[1]="0102";
cityname[1]="西城区";
cityid[2]="0103";
cityname[2]="崇文区";
cityid[3]="0104";
cityname[3]="宣武区";
cityid[4]="0105";
cityname[4]="朝阳区";
cityid[457]="3401";
cityname[457]="福田区";
cityid[458]="3402";
cityname[458]="罗湖区";
cityid[459]="3403";
cityname[459]="南山区";
cityid[460]="3404";
cityname[460]="宝安区";
cityid[461]="3405";
cityname[461]="龙岗区";
cityid[462]="3406";
cityname[462]="盐田区";
cityid[463]="3407";
cityname[463]="光明新区";
areaid[0]="广州01";
areaname[0]="请选择";
areaid[1]="广州02";
areaname[1]="越秀区";
areaid[2]="广州03";
areaname[2]="海珠区";
areaid[3]="广州04";
areaname[3]="荔湾区";
areaid[4]="广州05";
areaname[4]="天河区";
areaid[5]="广州06";
areaname[5]="白云区";
areaid[6]="广州07";
areaname[6]="黄埔区";
areaid[7]="广州08";
areaname[7]="花都区";
areaid[8]="广州09";
areaname[8]="番禺区";
areaid[9]="广州10";
areaname[9]="南沙区";
areaid[10]="广州11";
areaname[10]="萝岗区";
areaid[11]="广州12";
areaname[11]="从化市";
areaid[66]="广州13";
areaname[66]="增城市";
function LoadCity(pvalue,objListC)
{
var j=0;
objListC.length=cityid.length;
for (var i=0;i<cityid.length;i++)
{
if (cityid[i].substr(0,2)==pvalue)
{
objListC.options[j].text=cityname[i];
objListC.options[j].value=cityname[i];
j=j+1;
}
}
objListC.length=j;
}
function LoadArea(pvalue,objListC)
{
var j=0;
objListC.length=areaid.length;
for (var i=0;i<areaid.length;i++)
{
if (areaid[i].substr(0,2)==pvalue)
{
objListC.options[j].text=areaname[i];
objListC.options[j].value=areaname[i];
j=j+1;
}
}
if (j==0)
{
objListC.length=1;
objListC.options[0].text="";
objListC.options[0].value="";
}
else
{
objListC.length=j;
}
}
function SelectList(objList,idstring)
{
for (var i=0;i<objList.length;i++)
{
if (objList.options[i].value==idstring)
{
objList.options[i].selected = true;
return;
}
}
}
我这个实例是别人给我搞的.只有深圳和广东的有县市,其它都是省市,我想补全每个省的县.但从这个代码我还是看不懂他怎么关联的深圳的区和广东的县市.请高手赐教.由于内容长度限制,我删除了 展开
function LoadProvince(objList)
{
objList.length=provinceid.length;
for (var i=0;i<provinceid.length;i++)
{
objList.options[i].text=provincename[i];
objList.options[i].value=provinceid[i];
}
}
*/
var provinceid = new Array;
var provincename = new Array;
var cityid = new Array;
var cityname = new Array;
var areaid = new Array;
var areaname = new Array;
cityid[0]="0101";
cityname[0]="东城区";
cityid[1]="0102";
cityname[1]="西城区";
cityid[2]="0103";
cityname[2]="崇文区";
cityid[3]="0104";
cityname[3]="宣武区";
cityid[4]="0105";
cityname[4]="朝阳区";
cityid[457]="3401";
cityname[457]="福田区";
cityid[458]="3402";
cityname[458]="罗湖区";
cityid[459]="3403";
cityname[459]="南山区";
cityid[460]="3404";
cityname[460]="宝安区";
cityid[461]="3405";
cityname[461]="龙岗区";
cityid[462]="3406";
cityname[462]="盐田区";
cityid[463]="3407";
cityname[463]="光明新区";
areaid[0]="广州01";
areaname[0]="请选择";
areaid[1]="广州02";
areaname[1]="越秀区";
areaid[2]="广州03";
areaname[2]="海珠区";
areaid[3]="广州04";
areaname[3]="荔湾区";
areaid[4]="广州05";
areaname[4]="天河区";
areaid[5]="广州06";
areaname[5]="白云区";
areaid[6]="广州07";
areaname[6]="黄埔区";
areaid[7]="广州08";
areaname[7]="花都区";
areaid[8]="广州09";
areaname[8]="番禺区";
areaid[9]="广州10";
areaname[9]="南沙区";
areaid[10]="广州11";
areaname[10]="萝岗区";
areaid[11]="广州12";
areaname[11]="从化市";
areaid[66]="广州13";
areaname[66]="增城市";
function LoadCity(pvalue,objListC)
{
var j=0;
objListC.length=cityid.length;
for (var i=0;i<cityid.length;i++)
{
if (cityid[i].substr(0,2)==pvalue)
{
objListC.options[j].text=cityname[i];
objListC.options[j].value=cityname[i];
j=j+1;
}
}
objListC.length=j;
}
function LoadArea(pvalue,objListC)
{
var j=0;
objListC.length=areaid.length;
for (var i=0;i<areaid.length;i++)
{
if (areaid[i].substr(0,2)==pvalue)
{
objListC.options[j].text=areaname[i];
objListC.options[j].value=areaname[i];
j=j+1;
}
}
if (j==0)
{
objListC.length=1;
objListC.options[0].text="";
objListC.options[0].value="";
}
else
{
objListC.length=j;
}
}
function SelectList(objList,idstring)
{
for (var i=0;i<objList.length;i++)
{
if (objList.options[i].value==idstring)
{
objList.options[i].selected = true;
return;
}
}
}
我这个实例是别人给我搞的.只有深圳和广东的有县市,其它都是省市,我想补全每个省的县.但从这个代码我还是看不懂他怎么关联的深圳的区和广东的县市.请高手赐教.由于内容长度限制,我删除了 展开
1个回答
展开全部
写的似乎有点麻烦,不如直接在下拉列表的Option里添加。
我简单给你写个。
<script language="javascript">
var arr=new Array();
arr["广州01"]=["东城区","东城区","越秀区"];
arr["广州02"]=["萝岗区","天河区","南沙区"];
arr["广州03"]=["越秀区","盐田区","龙岗区"];
function Test()
{
var province=document.getElementById("province").value;
var district=document.getElementById("district");
//清空
district.length=1;
for(var i=0;i<arr[province].length;i++)
{
var opt=new Option(arr[province][i],arr[province][i]);
district.options.add(opt);
}
}
</script>
<body>
<select id="province" onchange="Test()">
<option>--请选择--</option>
<option value="广州01">广州01</option>
<option value="广州02">广州02</option>
<option value="广州03">广州03</option>
</select>
<select id="district"><option>--请选择--</option></select>
</body>
我简单给你写个。
<script language="javascript">
var arr=new Array();
arr["广州01"]=["东城区","东城区","越秀区"];
arr["广州02"]=["萝岗区","天河区","南沙区"];
arr["广州03"]=["越秀区","盐田区","龙岗区"];
function Test()
{
var province=document.getElementById("province").value;
var district=document.getElementById("district");
//清空
district.length=1;
for(var i=0;i<arr[province].length;i++)
{
var opt=new Option(arr[province][i],arr[province][i]);
district.options.add(opt);
}
}
</script>
<body>
<select id="province" onchange="Test()">
<option>--请选择--</option>
<option value="广州01">广州01</option>
<option value="广州02">广州02</option>
<option value="广州03">广州03</option>
</select>
<select id="district"><option>--请选择--</option></select>
</body>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询