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

Laravel populate select field after selecting county input

本文关键字:填充 选择 字段 输入 选择县 Laravel      更新时间:2023-09-26

我正在使用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')没有正确传递到路由?

通过替换解决

Input::get('county_id')

通过

Input::get('option')