选择县输入后,Laravel填充选择字段

时间:2014-09-26 16:18:22

标签: jquery ajax laravel

我正在使用Laravel 4

这是我的路线:

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为NULL,我无法弄清楚为什么Input::get('county_id')没有正确传递给路线?

1 个答案:

答案 0 :(得分:0)

这只是一个小错误,将Input::get('county_id')替换为Input::get('option')并且有效。