Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在laravel中创建动态选择选项?_Php_Laravel - Fatal编程技术网

Php 如何在laravel中创建动态选择选项?

Php 如何在laravel中创建动态选择选项?,php,laravel,Php,Laravel,我有两个选择,第一个选择由ouvrages填写,另一个选择由chantiers填写。它们之间存在关系,我希望当我在第一个选择chantier时,第二个选择由ouvrages填写,但在我的情况下,它会给我空数据 mois.blade.php //select chantiers <select class="form-control" id="chantier_id"> <option selected disabled>Select Location</opti

我有两个选择,第一个选择由ouvrages填写,另一个选择由chantiers填写。它们之间存在关系,我希望当我在第一个选择chantier时,第二个选择由ouvrages填写,但在我的情况下,它会给我空数据

mois.blade.php

//select chantiers
<select class="form-control" id="chantier_id">
  <option selected disabled>Select Location</option>
    @foreach($chantiers as $chantier)
  <option value="{{ $chantier->id }}">{{ $chantier->chantier}}</option>
    @endforeach
</select>
//select ouvrages
<select class="form-control" id="ouvrage_id">
   <option value="0" selected disabled>- Select -</option>
</select>
  public function select(request $request){
      $ouvrages = Ouvrage::where('chantier_id',$request->chantier_id);
        return response()->json(['options'=>$ouvrages]);
    }
Route::post('/select','SalarieController@select')->name('salarie.select'); 
Route::get('/mois','SalarieController@mois'); 
web.php

//select chantiers
<select class="form-control" id="chantier_id">
  <option selected disabled>Select Location</option>
    @foreach($chantiers as $chantier)
  <option value="{{ $chantier->id }}">{{ $chantier->chantier}}</option>
    @endforeach
</select>
//select ouvrages
<select class="form-control" id="ouvrage_id">
   <option value="0" selected disabled>- Select -</option>
</select>
  public function select(request $request){
      $ouvrages = Ouvrage::where('chantier_id',$request->chantier_id);
        return response()->json(['options'=>$ouvrages]);
    }
Route::post('/select','SalarieController@select')->name('salarie.select'); 
Route::get('/mois','SalarieController@mois'); 
您需要jQuery.parse(response)来解析json

并将loop更改为$。每个,以使其更简单、更简单

 <script type="text/javascript">
   $(document).ready(function() {
     $('#example').DataTable();

        $("#chantier_id").change(function(){
            var chantier = $(this).val(); 

            $.ajax({
                url: "{{ route('salarie.select') }}",  
                type: 'post',
                data: {
                      chantier:chantier,
                      "_token": "{{ csrf_token() }}"
                       },
                dataType: 'json',
                success:function(response){
                   console.log(jQuery.parse(response));
                    $("#ouvrage_id").empty();
                    $.each( jQuery.parse(response), function( key, value ) {
                      $("#ouvrage_id").append("<option value='"+key+"'>"+value+"</option>");
                     });

                }
            });
        });
    });
</script>

$(文档).ready(函数(){
$(“#示例”).DataTable();
$(“#chantier_id”).change(函数(){
var chantier=$(this.val();
$.ajax({
url:“{route('salarie.select')}}”,
键入:“post”,
数据:{
尚蒂埃:尚蒂埃,
_令牌“{{csrf_令牌()}}”
},
数据类型:“json”,
成功:功能(响应){
log(jQuery.parse(response));
$(“#ouvrage_id”).empty();
$.each(jQuery.parse(响应)、函数(键、值){
$(“#ouvrage_id”)。追加(“+value+”);
});
}
});
});
});

您确定有数据返回吗?而且我猜您需要JSON.parse(response),因为JSON返回和for循环,您应该使用$.each()来循环响应,您不需要迭代器i thenthx作为答案,但它会给我这个错误SyntaxError:JSON.parse:JSON数据第1行第2列的意外字符我修复了它,use jQuery.parsejQuery.parse不是一个函数,除非您使用的是旧的jQuery版本,或者它已经被解析。尝试不使用它,只需在loopSuny ouvrage中使用response选择give me[Object Object]