jquery+php后台实现省市区联动功能示例
发布时间:2020-09-03 19:24:16
来源:亿速云
阅读:99
作者:栢白
本篇文章和大家了解一下jquery+php后台实现省市区联动功能示例。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。
jquery是什么
jquery是一个简洁而快速的JavaScript库,它具有独特的链式语法和短小清晰的多功能接口、高效灵活的css选择器,并且可对CSS选择器进行扩展、拥有便捷的插件扩展机制和丰富的插件,是继Prototype之后又一个优秀的JavaScript代码库,能够用于简化事件处理、HTML文档遍历、Ajax交互和动画,以便快速开发网站。
具体如下:
JS:$("#from_prov_id,#from_city_id").change(function () {
var current_prov_id = $(this).val();
var _this = this;
$.ajax({
type:'get',
url:"{:url('getarea')}?id="+current_prov_id,
success:function (data) {
var data = JSON.parse(data);
if (data.list2==''){
$('#from_area_id option').remove();
$("#from_area_id").append( '选择区(县)');
$.each(data.list1,function (index,item) {
$("#from_area_id").append( ''+item.name+'');
});
}else{
$("#from_city_id option").remove();
$('#from_area_id option').remove();
$("#from_city_id").append( '选择城市');
$.each(data.list1,function (index,item) {
$("#from_city_id").append( ''+item.name+'');
});
$("#from_area_id").append( '选择区(县)');
$.each(data.list2,function (index,item) {
$("#from_area_id").append( ''+item.name+'');
});
}
}
})
});
html:
选择省份
北京
上海
浙江
四川
山西
江苏
{/volist}
选择城市
选择区县
php:public function area(){
$area = new Area();
$id = input('id');
$list = $area->getList(['pid'=>$id]);
$list2 = $area->getList(['pid'=>$list[0]['id']]);
return json_encode(['list1'=>$list,'list2'=>$list2]);
}
以上就是jquery+php后台实现省市区联动功能示例的简略介绍,当然详细使用上面的不同还得要大家自己使用过才领会。如果想了解更多,欢迎关注亿速云行业资讯频道哦!