2017-04-26 51 views
0

我試圖做一個從屬下拉列表,但我不太確定如何做到這一點,所以一點幫助將是非常欣賞。此代碼顯示錯誤「未定義的變量:輸入「。 enter image description here對於第一個下拉菜單,我有3個靜態選項。從屬下拉列表3個不同的表

<option value="business">Business</option> 
<option value="branch">Branch</option> 
<option value="offer">Offer</option> 

對於第二次下拉,我需要根據先選擇什麼來選擇。 例如,如果用戶選擇業務 在第二個下拉,他需要從列表

@foreach($businesses as $business) 
<option value="{{$business->id}}">{{$business->name}}</option> 
@endforeach 

等選擇特定的業務,所以如果他選擇要約我需要的所有報價 我的列表跟着關於ajax的教程,但現在我陷入了困境。 這是我的看法

<div class="form-group"> 
    <label class="control-label col-lg-12">Type <span class="text-danger">*</span></label> 
    <div class="col-lg-12"> 
     {{ Form::open() }} 
     <select rows="5" cols="5" name="type_id" id="type_id" class="form-control" required="required" placeholder="Default textarea"> 
      <option value="business">Business</option> 
      <option value="branch">Branch</option> 
      <option value="offer">Offer</option> 
     </select> 
    </div> 
</div> 

<div class="form-group"> 
    <label class="control-label col-lg-12">Offer <span class="text-danger">*</span></label> 
    <div class="col-lg-12"> 

     <select id="series" name="series"> 
      @if($input=='business') 
       @foreach($businesses as $business) 

        <option value="{{$business->id}}">{{$business->name}}</option> 
       @endforeach 


       @elseif($input=='branch') 
        @foreach($branches as $branch) 

         <option value="{{$branch->id}}">{{$branch->name}}</option> 
        @endforeach 

       @else 
       @foreach($offers as $offer) 

        <option value="{{$offer->id}}">{{$offer->title}}</option> 
       @endforeach 
       @endif 
     </select> 
     {{ Form::close()}} 
    </div> 

腳本

<script> 

    $(document).ready(function($){ 

     $('#type_id').change(function(){ 

      $.get("{{ url('api/repairdropdown')}}", { option: $(this).val() }, 

        function(data) { 

         var numbers = $('#series'); 

         numbers.empty(); 

         $.each(data, function(key, value) { 

          numbers 
            .append($("<option></option>") 
            .attr("value",key) 
            .text(value)); 
         }); 

        }); 

     }); 

    }); 
</script> 

和我的控制器

public function dropdown() 
{ 

    $input = Input::get('option'); 
    $offers = Offers::where([ 
     'visible' => 'yes', 
     'delete' => 'no' 
    ])->orderBy('id', 'DESC') 
     ->get(); 

    $businesses = Offers::where([ 
     'visible' => 'yes', 
     'delete' => 'no' 
    ])->orderBy('id', 'DESC') 
     ->get(); 

    $branches = Branches::where([ 
     'visible' => 'yes', 
     'delete' => 'no' 
    ])->orderBy('id', 'DESC') 
     ->get(); 
    return Response::json($offers,$businesses,$branches,$input); 

} 

謝謝你,如果你停下來!

+0

你希望所有的'$ businesses','$ branches'單下拉列表中? –

回答

0

試試這個:

控制器

public function dropdown() 

{

$input = Input::get('option'); 
$offers = Offers::where([ 
    'visible' => 'yes', 
    'delete' => 'no' 
])->orderBy('id', 'DESC') 
    ->get(); 

$businesses = Offers::where([ 
    'visible' => 'yes', 
    'delete' => 'no' 
])->orderBy('id', 'DESC') 
    ->get(); 

$branches = Branches::where([ 
    'visible' => 'yes', 
    'delete' => 'no' 
])->orderBy('id', 'DESC') 
    ->get(); 
if($input == 'Business'){ 
    $dropdownData = $businesses; 
}elseif($input == 'Branches'){ 
    $dropdownData = $branches; 
} 

$output = ''; 
foreach($dropdownData as $value){ 
    $output.="<option value='".$value->id."'>".$value->business_name."</option>" 
} 
echo $output; 

}

JavaScript代碼

<script> 

$(document).ready(function($){ 

    $('#type_id').change(function(){ 

     $.get("{{ url('api/repairdropdown')}}", { option: $(this).val() }, 

       function(data) { 

        $('your_select_id').append(data); 


       }); 

    }); 

});