三级联动Linkage

HTML

<select id="top">
    <option>顶级分类</option>
    {foreach($data as $value)}
    <option value="{$value['id']}">{$value['cate_name']}</option>
    {/foreach}
</select>
<select id="cate_two">
    <option>二级分类</option>
</select>
<select id="cate_three">
    <option>三级分类</option>
</select>

JQuery

<script>
    //顶级分类与二级分类联动
    $('#top').change(function () {
        var id = $(this).val();
        $('#cate_two').children('option:gt(0)').remove();
        $('#cate_three').children('option:gt(0)').remove();
        $.ajax({
            url: 'duojiTwo?id=' + id,
            dataType: 'json',
            success: function (res) {
                if (res.code !== 200) {
                    alert(res.msg);
                    $('#cate_two').children('option:gt(0)').remove();
                    $('#cate_three').children('option:gt(0)').remove();
                }
                $.each(res.data, function (index, ele) {
                    $('#cate_two').append("<option value=" + ele.id + ">" + ele.cate_name + "</option>");
                })
            }
        })
    })

    //二级分类与三级分类联动
    $('#cate_two').change(function () {
        var id = $(this).val();
        $('#cate_three').children('option:gt(0)').remove();
        $.ajax({
            url: 'duojiThree?id=' + id,
            dataType: 'json',
            success: function (res) {
                if (res.code !== 200) {
                    alert(res.msg);
                    // $('#cate_three').html("<option>" + '三级分类' + "</option>");
                    $('#cate_three').children('option:gt(0)').remove();
                }
                $.each(res.data, function (index, ele) {
                    $('#cate_three').append("<option value=" + ele.id + ">" + ele.cate_name + "</option>");
                })
            }
        })
    })
</script>

PHP

public function liandong()
    {
        $data = Cate::where('pid', '=', 0)->select()->toArray();
        return view('/liandong', ['data' => $data]);
    }

    public function liandongTwo()
    {
        $id = \request()->get('id');
        if (!is_numeric($id)) {
            return json([
                'code' => 2001,
                'msg' => '请先选择顶级分类'
            ]);
        }
        $data = Cate::where('pid', '=', $id)->field(['cate_name', 'id'])->select()->toArray();
        return json([
            'code' => 200,
            'msg' => '查找成功',
            'data' => $data
        ]);
    }

    public function liandongThree()
    {
        $id = \request()->get('id');
        if (!is_numeric($id)) {
            return json([
                'code' => 2001,
                'msg' => '请先选择二级分类'
            ]);
        }
        $data = Cate::where('pid', '=', $id)->field(['cate_name', 'id'])->select()->toArray();
        return json([
            'code' => 200,
            'msg' => '查找成功',
            'data' => $data
        ]);
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值