php tp5 實現三級聯動
阿新 • • 發佈:2022-04-19
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; }