【问题标题】:Dynamic Dependent Drop down selection in LaravelLaravel 中的动态相关下拉选择
【发布时间】:2020-09-25 03:58:03
【问题描述】:

大家好,我正在研究动态相关下拉选择,但我无法实现所需的输出,当我在 State 列中选择某些内容时,city 列始终为空。

这是我的控制器specvil.php

 public function  index  () {


        $ville=Vil::orderBy('Ville','desc')->get();

        return view ('index',['vi'=>$ville]);
    } 

public function deleg(Request $request)

    {
        $delegation = DB::table("delegation")
        ->where("ville_id",$request->ville_id)
        ->pluck("ville","id");
        return response()->json($delegation);
    }

这是 index.php 表单的视图:

 <form action="{{route('ghof')}}" method="get"> 
                         {{ csrf_field() }}
                          <select type="text" class="search-field location" name="spec" id="s" 
                         value="spec" placeholder ="Spécialités">
                         <option selected></option>
                         @foreach($sp as $ss)
                         <option value=" {{$ss->Spécialité}}"> {{$ss->Spécialité}}</option>
                         @endforeach
                         </select> 
                         <select type="text" class="search-field location" name="Région" id="Région" 
                         value="Région">
                         <option selected></option>
                         @foreach($vi as $vv)
                         <option value="{{$vv->Ville}}">{{$vv->Ville}}</option>
                         @endforeach
                         </select>
                         <select type="text" class="search-field location" name="ville" id="ville">
                         </select>
                         <button class="search-btn" type="submit" id="search"> Recherche </button>
                        </div>
                        </form>

这是脚本:

<script type="text/javascript">
    $('#Région').change(function(){
    var countryID = $(this).val();    
    if(countryID){
        $.ajax({
           type:"GET",
           url:"{{url('deleg')}}?ville_id="+countryID,
           success:function(res){               
            if(res){
                $("#ville").empty();
                $("#ville").append('<option>Select</option>');
                $.each(res,function(key,value){
                    $("#ville").append('<option value="'+key+'">'+value+'</option>');
                });

            }else{
               $("#ville").empty();
            }
           }
        });
        </script>

最后这是路线:

Route::get('/index','specvil@index');
Route::get('deleg','specvil@deleg');

【问题讨论】:

    标签: php jquery ajax laravel forms


    【解决方案1】:

    也许这就是问题所在。您将 Ville desc 作为选项值传递,而不是传递 Ville id,这会导致您在控制器的 deleg() 方法中将 Ville desc 与 Ville id 匹配。

    【讨论】:

    • 我现在还有一个问题,Uncaught ReferenceError: module is not defined
    猜你喜欢
    • 2020-06-29
    • 2021-03-04
    • 2020-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-30
    • 2020-01-06
    • 2019-06-04
    相关资源
    最近更新 更多