我有两个下拉城市和城市区域。我想根据选定的City值填充City area下拉列表。
路线:
Route::get('create-profile', 'ProfileController@index');
Route::get('create-profile/city_area/{id}', 'ProfileController@city_area');
主要下拉列表是:城市
<div class="col-xs-7 col-md-push-1">
<select name="city" id="city">
<option value="">--- Select City ---</option>
@foreach($city as $city)
<option value="{{$city->id}}">{{$city->city_name}}</option>
@endforeach
</select></div>
依赖下拉是
<div class="inp_sec">
<div class="col-xs-4">
<label for="r3">City Area</label>
</div><div class="col-xs-7 col-md-push-1">
<select name="city_area" id="city_area"></select></div></div>
dependend下拉列表的Ajax是
<script type="text/javascript">
$(document).ready(function() {
$('select[name="city"]').on('change', function() {
var cityID = $(this).val();
if(cityID) {
$.ajax({
url: 'create-profile/city_area/'+cityID,
type: "GET",
dataType: "json",
success:function(data) {
$('select[name="city_area"]').empty();
$.each(data, function(key, value) {
$('select[name="city_area"]').append('<option value="'+ key +'">'+ value +'</option>');
});
}
});
}else{
$('select[name="city_area"]').empty();
}
});
});
获取依赖下拉数据的Php代码是
public function city_area($id)
{
$city_area= DB::table('czech_area')->where('citi_id',$id)-
>select('id','area_name')->get();
return json_encode($city_area);
}
答案 0 :(得分:0)
当您看到 [对象对象] 时,它是对象到字符串转换的明确标志。
city_area()函数返回稍后使用JSON编码的对象列表。因此,用于填充 city_area 下拉列表的值变量是Javascript对象,而不是字符串。您需要在您的案例中指定要使用的此对象的属性 - id 和 area_name :
$('select[name="city_area"]').append('<option value="'+ value.id +'">'+ value.area_name +'</option>');