【问题标题】:Laravel populate select field after selecting county input选择县输入后,Laravel 填充选择字段
【发布时间】:2014-09-26 16:18:21
【问题描述】:

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

【问题讨论】:

  • 你在哪里把county_id 传递给路由?
  • 在 getJson 中:option: $(this).val()
  • 这可能是你的问题。如果您发送名称为option 的参数,Input::get('county_id') 将如何工作?请改用Input::get('option');。
  • 是的,谢谢,我想通了!

标签: javascript php jquery ajax laravel


【解决方案1】:

通过替换解决

Input::get('county_id')

由

Input::get('option')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-31
    • 2023-04-09
    • 2021-12-10
    • 2023-03-21
    • 1970-01-01
    • 2020-09-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多