laravel 里面的2级联动

1.控制器代码
 public function add(Request $request){
        if($request->isMethod('post')){
            $file = files_upload('goods_img',public_path('/admin/goods/'),['jpg','jpeg','gif','png'],'/admin/goods/');
            if ($file['status']) {
                $data['goods_img'] = serialize($file['path']);
                $data['goods_name'] = $request->input('goods_name');
                $data['sale_price'] = $request->input('sale_price');
                $data['goods_price'] = $request->input('goods_price');
                $data['sale_number'] =$request-> input('sale_number');
                $data['number'] = $request->input('number');
                $data['kg'] = $request->input('kg');
                $data['goods_type'] = $request->input('goods_type');
                $data['goods_describe'] =$request-> input('goods_describe');
                $data['goods_content'] =$request-> input('goods_content');
                $data['goods_state'] = $request->input('goods_state');
                $data['price'] =$request-> input('price');
                $data['add_time']=date("Y-m-d",time());
                GoodsModel::create($data);
                return redirect('/admin/goods/index');
            }


        }
           if($request->ajax()){
                $cat_id = htmlentities($request->input('cat_id'));//类别编号
                $cat_num = htmlentities($request->input('cat_num'));//判断第几个级别
                if($cat_id == 0){
                    return;
                }
                if($cat_num == 1){
                    //第一级别
                    $two_cate=GoodstypeModel::where('cid',$cat_id)->get();
                    if($two_cate){
                        $json = json_encode($two_cate);
                        return ['sta'=>2,'info'=>$json];
                    }else{
                        return ['sta'=>1,'info'=>'无子分类,请添加!'];
                    }
                }elseif($cat_num == 2){
                    //第二级别
                    $two_cate=GoodstypeModel::where('cid',$cat_id)->get();
                    if($two_cate){
                        $json = json_encode($two_cate);
                        return ['sta'=>2,'info'=>$json];
                    }else{
                        return ['sta'=>1,'info'=>'无子分类,请添加!'];
                    }
                }
            }
            $data=GoodstypeModel::where('cid',0)->get()->toarray();
            return view('/admin/goods/add',['data'=>$data]);


    }
2.添加页面的JS

<script type="text/javascript" src="{{asset('/admin_page/lib/jquery/1.9.1/jquery.min.js')}}"></script>
<script type="text/javascript">
    $("#select_cat1").change(function(){
        $("#select_cat2").html("");//清除
        // $("#select_cat3").html("");//清除
        $("#select_cat2").append("<option value='0'>请选择</option>");
        // $("#select_cat3").append("<option value='0'>请选择</option>");
        var cat1 = $(this).val();
        $.ajax({
            url:"/admin/goods/add",
            type:'get',
            dataType:'json',
            data:{cat_id:cat1,cat_num:1},
            success:function(data){
                if(data.sta == 1){
                    alert(data.info);return;
                }
                if(data.sta == 2){
                    str = JSON.parse(data.info);//json转数组
                    for (var i = str.length - 1; i >= 0; i--) {
                        $("#select_cat2").append("<option value='"+str[i]['id']+"'>"+str[i]['name']+"</option>");
                    };
                }
            }
        });
    })
    $(document).on('click','.deletediv',function(){
        $(this).parent().remove();
    });
    function up1(){
        var select_cat1 = $("#select_cat1").val();
        var select_cat2 = $("#select_cat2").val();
        var goods_img_arr = $("input[name='goods_img_arr[]']").val();
        if(select_cat1 == 0){alert("一级分类不能为空!");return false;}
        if(select_cat2 == 0){alert("二级分类不能为空!");return false;}
        if(!goods_img_arr){alert("图片不能为空!");return false;}
    }
    UE.getEditor('image',{initialFrameWidth:800,initialFrameHeight:200})

</script>

3.选框代码

<div class="row cl">
    <label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>选择商品类别:</label>
    <div class="formControls col-xs-8 col-sm-9">
        <div  style="width: 20%;">
            <select class="select"  size="1" id="select_cat1" name="select_cat1">
                <option value="0">请选择</option>
                @foreach($data as $k=>$v)
                        <option value="{{$v['id']}}">{{$v['name']}}</option>
                @endforeach
            </select>

        </div>
        <div  style="width: 20%;">
        <select class="select"  size="1" id="select_cat2" name="select_cat2">
            <option value="0">请选择</option>
        </select>
        </div>
    </div>
</div>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值