//三级联动
//数据库代码过多就不上传了
//视图
<div class="area">
<table class="table">
<select name="region[]" id="">
<option value="0">请选择</option>
<?php foreach ($regiondata as $value):?>
<option value="<?php echo $value['region_id'];?>"><?php echo $value['region_name'];?></option>
<?php endforeach ?>
</select>
</table>
</div>
//jquery
<script>
//委托事件
$(document).on('change',":input[name='region[]']",function(){
var region_id=$(this).val();
console.log(region_id);
//注意:此时的_this是为了下面时使用
var _this=$(this);
var url="?r=test/nextregion";
$.getJSON(url,{'region_id':region_id},function(msg){
// alert(msg);
if(msg.length>0&®ion_id!=0){
var str="<select name='region[]'><option value=''>请选择</option>";
//利用each循环拼接json数据
$(msg).each(function(k,v){
str+="<option value="+v.region_id+">"+v.region_name+"</option>";
})
str+="</select>";
}
//
_this.nextAll().remove();
_this.after(str);
});
});
</script>
//控制器页面
public function actionAreacon(){
$regiondata= (new yiidbQuery())
->select('region_id,region_name')
->from('region')
->where('parent_id=0')
->all();
return $this->render('areacon',[
'regiondata'=>$regiondata,
]);
}
//处理三级联动
//控制器对于get数据的处理和返回值(我的是利用YII框架,查询方式不同而已)
//查询下级地区
public function actionNextregion(){
$request=YII::$app->request;
//接收传过来的region_id(地区id)作为(地区父级)条件查询
$region_id=$request->get('region_id');
$regiondata= (new yiidbQuery())
->select('region_id,region_name')
->from('region')
->where('parent_id=:region_id',[':region_id'=>$region_id])
->all();
echo json_encode($regiondata);die;
}