由于项目的需求需要写一个地区联动的功能,在网上找了一个地址库,放到了数据库中弄了大半天终于实现了想要的功能。废话不多说了将源码发出来。
查找出省级城市
“执行代码:
| 代码如下 |
复制代码 |
|
<?php
$area =M("Area");
$privince = $area->where("region_type = 1")->select();
$this->assign("privince",$privince);
?>
|
javascript代码:
| 代码如下 |
复制代码 |
|
function handleStateChange(){
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
if(requestType=="city"){
showcity();
}
else if(requestType="area"){
showarea();
}
}
}
}
function queryCity(citycode){
createXMLHttpRequest();
requestType="city";
var url="/esoxue.php/Crm/area/provincecode/"+citycode+"/"+Math.random();
xmlHttp.open("GET",url,true);
xmlHttp.onreadystatechange=handleStateChange;
xmlHttp.send(null);
}
function queryArea(citycode){
createXMLHttpRequest();
requestType="area";
var url="/esoxue.php/Crm/area/citycode/"+citycode+"/"+Math.random();
xmlHttp.open("GET",url,true);
xmlHttp.onreadystatechange=handleStateChange;
xmlHttp.send(null);
}
function showcity(){
document.getElementById("city").innerHTML=xmlHttp.responseText;//捕获ID显示返回的数据
}
function showarea(){
document.getElementById("area").innerHTML=xmlHttp.responseText;//捕获ID显示返回的数据
}
html代码:
请选择地区
{$vo.region_name}
执行代码:
public function area()
{
$area =M('Area');
$provincecode=$_GET['provincecode'];//接收省键值
$citycode=$_GET['citycode'];//接收城市键值
if($provincecode !=""){
$citys=$area->where("parent_id='$provincecode'")->select();
echo "";
echo "请选城市n";
foreach ($citys as $key=>$val ){
echo "{$citys[$key]['region_name']}";
}
}
if($citycode!=""){
$areas=$area->where("parent_id='$citycode'")->select();
if(!empty($areas)){
echo "n";
echo "请选择县n";
foreach ($areas as $key=>$val ){
echo "{$areas[$key]['region_name']}";
}
echo "n";
}else{
$areaname =$area->where("region_id ='$citycode'")->field('region_name')->find();
echo "n";
echo "请选择县n";
echo "{$areaname['region_name']}";
echo "n";
}
}
}
|
效果图如下:
jquery18305617892363434878="45" />
最终效果图:
