Laravel+ Vue+实现 + 三级联动!!!

客户端

首先引入vue+Ajax

  <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
    <script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js"></script>
  <label for="pwd">公司地址:</label>
            <select name="" id="" style="width: 150px" @change="getqu()" v-model="quid">
                <option :value="item.id"   v-for="item in city">@{{ item.name }}</option>
            </select>
            <select name="" id="" style="width: 150px" @change="getxian()"   v-model="xianid">
                <option :value="item.id"   v-for="item in qu">@{{ item.name }}</option>
            </select>
            <select name="" id="" style="width: 150px" >
                <option :value="item.id"   v-for="item in zhen">@{{ item.name }}</option>
            </select>

Vue

<script>
    new Vue({
        el:'#container',
        data:{
            city:null,
            qu:'',
            zhen:'',
            quid:'',
            xianid:''
        },
        mounted () {
            axios
                .get("{{url('getCity')}}")
                .then(response => (this.city = response.data.data))
                .catch(function (error) { // 请求失败处理
                    console.log(error);
                });
        },
        methods:{
         getqu () {
            // console.log(this.quid);
             axios
                 .get("{{url('getQu')}}?id="+this.quid)
                 .then(response => (this.qu = response.data.data))
                 .catch(function (error) { // 请求失败处理
                     console.log(error);
                 });
            },
            getxian(){
                axios
                    .get("{{url('getQu')}}?id="+this.xianid)
                    .then(response => (this.zhen = response.data.data))
                    .catch(function (error) { // 请求失败处理
                        console.log(error);
                    });
            }
        }
    })
</script>

服务端

 public function getCity(){
        $data = City::where('pid',0)->get()->toArray();
       return ['code'=>200,'msg'=>'seccess','data'=>$data];

    }

    public function getQu(Request $request)
    {
        $data = City::where('pid',$request->get('id'))->get()->toArray();
        return ['code'=>200,'msg'=>'seccess','data'=>$data];
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值