php部分
$area1 = $this->getAreaList(0); $this->assign("area1",$area1);
public function changeArea3(){ $area = $this->getAreaList($_POST["areaID"]); $html = ""; foreach($area as $v){ $html .='<option value="'.$v['areaID'].'">'.$v['name'].'</option>'; } ajaxReturn($html,'',1); }
public function getAreaList($parent=0){ return db("area_info")->where("parent=".$parent)->order("areaID ASC")->select(); }
jquery部分
var areaID = 0; function selectArea(type){ areaID = $("#select-"+type+" option:selected").val(); if(areaID == 0){ if(type == 1){ $("#select-2").html("<option value='0'>市</option>"); $("#select-3").html("<option value='0'>县区</option>"); areaID = 0; } if(type == 2){ $("#select-3").html("<option value='0'>县区</option>"); areaID = $("#select-1").val(); } if(type == 3){ areaID = $("#select-2").val(); } return; } if(type ==3){ return ; } $.ajax({ type:'POST', dataType:'json', data:{'areaID':areaID,'type':type}, url:'{$Think.config.url}/index/Dnaagent/changeArea3', success:function(data){ if(data.status == 1){ if(type == 1){ $("#select-2").html(data.data); $("#select-3").html("<option value='0'>县区</option>"); } if(type == 2){ $("#select-3").html(data.data); } } } }) }
html部分
<div> <div class="btn-group margin-top10"> <select class="form-control" id="select-1" οnchange="selectArea(1)"> <option value="0">省直辖市</option> {volist name="area1" id="vo"} <option value="{$vo.areaID}">{$vo.name}</option> {/volist} </select> </div> <div class="btn-group margin-top10"> <select class="form-control" id="select-2" οnchange="selectArea(2)"> <option>市</option> </select> </div> <div class="btn-group margin-top10"> <select class="form-control" id="select-3" οnchange="selectArea(3)"> <option>县区</option> </select> </div> </div>