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')
沒有正確傳遞到路線?
你在哪裏將'county_id'傳遞給路由? – 2014-09-26 16:27:50
getJson:'option:$(this).val()' – PhilMarc 2014-09-26 16:30:06
這可能是你的問題。如果你發送的參數名稱是「option」,Input :: get('county_id')'如何工作?試試'Input :: get('option');'而不是。 – user3158900 2014-09-26 16:58:25