我在表单中使用了动态下拉列表
我要根据所选城市选择一个地区
这是我的观点
<div class="form-group col-md-6 form-group{{ $errors->has('city') ? ' has-error' : '' }}">
<label for="inputPassword4" style="padding-left:35em"><span style="color:red">*</span> city</label>
<select id="city" name="city" class="form-control">
<option ></option>
@foreach($cities as $city)
<option value="{{$city->id}}">{{$city->city}}</option>
@endforeach
</select>
<h6 class="text-danger">{{ $errors->first('city') }}</h6>
</div>
</div>
<div class="form-group col-md-6 form-group{{ $errors->has('area') ? ' has-error' : '' }}">
<label for="inputEmail4" style="padding-left:50em">area</label>
<select id="area" name="area" class="form-control">
<option></option>
</select>
<h6 class="text-danger">{{ $errors->first('area') }}</h6>
</div>
这是脚本
$('#city').on('change',function(e){
console.log(e);
var city= e.target.value;
$.get('/areas/'+ city, function (data){
//console.log(data);
$('#area').empty();
$.each(data,function(i,area){
$('#area').append('<option value ="'+area+'">'+area+'</option>');
});
});
});
</script>e
控制器
public function areas($id)
{
$area = area::where('city_id', $id)->pluck("area","id");
return Response::json($area);
}
路线:
Route::get('/areas/{id}', 'locationController@areas');
我使用了这段代码,但是没有用!
答案 0 :(得分:0)
使用return Response::json($area);
代替return response()->json($cities);
您的控制者将是:
public function areas($id)
{
$area = area::where('city_id', $id)->pluck("area","id");
return response()->json($area);
}