省市区三级联动 jquery tp5

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>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值