2017-04-26 50 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); 


       }); 

    }); 

});