thinkphp+ajax 实现地区联动效果实例

作者:袖梨 2022-06-25

由于项目的需求需要写一个地区联动的功能,在网上找了一个地址库,放到了数据库中弄了大半天终于实现了想要的功能。废话不多说了将源码发出来。

查找出省级城市

“执行代码:

代码如下 复制代码

$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代码:






执行代码:

public function area()
{
$area =M('Area');
$provincecode=$_GET['provincecode'];//接收省键值
$citycode=$_GET['citycode'];//接收城市键值
if($provincecode !=""){
$citys=$area->where("parent_id='$provincecode'")->select();
echo "n";
echo "n";
foreach ($areas as $key=>$val ){
echo "";
}
echo "n";
}else{
$areaname =$area->where("region_id ='$citycode'")->field('region_name')->find();
echo "n";
}
}
}

效果图如下:

thinkphp+ajax 实现地区联动效果实例jquery18305617892363434878="45" />

最终效果图:

thinkphp+ajax 实现地区联动效果实例

相关文章

精彩推荐