php tp5 实现三级联动
html
<div class="zsjm_one_one_one"> <span class="zsjm_one_one_one_span">所在地区: </span> <div class="zsjm_one_one_one_one"> <select id="pro" class="lv_sid"> <option>请输入您的省份</option> </select> </div> <div class="zsjm_one_one_one_one"> <select class="lv_sid" required id="city"> <option>请输入您的市区</option> </select> </div> <div class="zsjm_one_one_one_one"> <select class="lv_sid" required id="county"> <option>请输入您的县区</option> </select> </div> </div>
js
/** 页面加载时 查省 */ $.post( "{:url('provice')}", {act: "pro"}, function (msg) { // console.log(msg); var option = "<option>--请选择--</option>"; for (var i = 0; i < msg.length; i++) { option += "<option value='" + msg[i]['id'] + "'>" + msg[i]['region_name'] + "</option>"; } $("#pro").html(option); }, 'json' ) /*省发生改变 查市*/ $("#pro").change(function () { var pro_id = $("#pro option:selected").val(); // $("#city").empty(); $.post( "{:url('provice')}", {act: "city", id: pro_id}, function (msg) { var option = "<option>--请选择--</option>"; for (var i = 0; i < msg.length; i++) { option += "<option value='" + msg[i]['id'] + "'>" + msg[i]['region_name'] + "</option>"; } $("#city").html(option); }); }) /*市发生改变 查县*/ $("#city").change(function () { var pro_id = $("#city option:selected").val(); $.post( "{:url('provice')}", {act: "county", id: pro_id}, function (msg) { var option = "<option>--请选择--</option>"; for (var i = 0; i < msg.length; i++) { option += "<option value='" + msg[i]['id'] + "'>" + msg[i]['region_name'] + "</option>"; } $("#county").html(option); }); })
php
//省市县三级联动 public function provice(){ $act = input('post.'); if($act['act'] == "pro"){ $res = db('shopcity') -> where(['pid'=>0]) -> select(); }else if($act['act'] == "city"){ $res = db('shopcity') -> where(['pid'=>$act['id']]) -> select(); }else if($act['act'] == "county"){ $res = db('shopcity') -> where(['pid'=>$act['id']]) -> select(); } // var_dump($res);die; return $res; }