【发布时间】: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