我正在尝试在Laravel 4中进行动态下拉列表,但子类别不会显示列表
我的控制器
public function myform()
{
$bioskop = Bioskop::where('flag','=',1)->lists('namaBioskop','idBioskop');
$film=Film::where('flag','=',1)->lists('judulFilm','idFilm');
return View::make('pengelolaan.tayang.createtayang',compact('bioskop','film'));
}
/**
* Get Ajax Request and restun Data
*
* @return \Illuminate\Http\Response
*/
public function myformAjax($idBioskop)
{
$studio = Studio::where('idBioskop','=',$idBioskop)->where('flag','=',1)->where('active','=',1)->lists('namaStudio','idStudio');
return json_encode($studio);
}
路线
Route::get('myform',array('as'=>'myform','uses'=>'TayangController@myform'));
Route::get('myform/ajax/{id}',array('as'=>'myform.ajax','uses'=>'TayangController@myformAjax'));
视图
{{Form::open(array('url'=>'myform','method'=>'POST'))}}
<div class="form-group">
<div class="col-lg-6">
<label for="title">Bioskop </label>
<select name="bioskop" class="form-control">
@foreach ($bioskop as $key => $value)
<option value="{{ $key }}">{{ $value }}</option>
@endforeach
</select>
</div>
</div>
<div class="form-group">
<div class="col-lg-6">
{{ Form::label('idFilm', 'Film') }}
{{ Form::select('idFilm', $film,'',array('class'=>'form-control')) }}
</div>
</div>
<div class="form-group">
<div class="col-lg-6">
<label for="title">Studio</label>
<select name="studio" class="form-control"></select>
</div>
</div>
的javascript
<script type="text/javascript">
$(document).ready(function() {
$('select[name="bioskop"]').on('change', function() {
var idBioskop = $(this).val();
if(idBioskop) {
$.ajax({
url: '/myform/ajax/'+idBioskop,
type: "GET",
dataType: "json",
success:function(data) {
$('select[name="studio"]').empty();
$.each(data, function(key, value) {
$('select[name="studio"]').append('<option value="'+ key +'">'+ value +'</option>');
});
}
});
}else{
$('select[name="studio"]').empty();
}
});
});
父类别正在显示,但是当我选择父类别时,子类别将不会显示基于父类别的列表,它什么都不显示。有人可以帮忙吗?提前谢谢