2014-09-26 86 views
0

我使用Laravel 4Laravel选择县城输入

这里后填充select场是我的路线:

Route::get('api/dropdown/cities', function(){ 
    $county = Input::get('county_id'); 
    $cities = Place::where('parent_id', $county)->get(array('id','name')); 
    return Response::json($cities, 200); 
}); 

我的脚本:

$(document).ready(function() 
{ 
    $('#county_id').change(function(){ 
     $.getJSON("{{ url('api/dropdown/cities')}}", 
      { option: $(this).val() }, 
      function(data) { 
       var model = $('#city_id'); 
       model.empty(); 
       $.each(data, function(index, element) { 
        model.append("<option value='"+element.id+"'>" + element.name + "</option>"); 
       }); 
      }); 
    }); 
}); 

我的形式:

{{ Form::select('county_id', $counties, (isset($data['county_id'])) ? $data['county_id'] : null, array('id' => 'county_id')) }} 

<select id="city_id" name="city_id"> 
    <option>Select a county first</option> 
</select> 

它几乎在工作,因为Laravel Debugbar返回SQL查询:

select `id`, `name` from `places` where `parent_id` is null 

但正如你所看到的,parent_id是空的,我想不通为什么Input::get('county_id')没有正确传递到路线?

+0

你在哪里将'county_id'传递给路由? – 2014-09-26 16:27:50

+0

getJson:'option:$(this).val()' – PhilMarc 2014-09-26 16:30:06

+0

这可能是你的问题。如果你发送的参数名称是“option”,Input :: get('county_id')'如何工作?试试'Input :: get('option');'而不是。 – user3158900 2014-09-26 16:58:25

回答

0

这只是一个小小的错误,用Input::get('option')代替Input::get('county_id'),它的工作原理。