因此,基本上,我想在表单上创建一个动态下拉列表。
1个sasaran有许多指示符,所以我想要的是,当我单击sasaran中的1个时,indikator的下拉菜单将显示属于该sasaran的指示符。 这是我的看法:
<div class="form-group col-md-5">
<label for="sasaran_id">Sasaran</label>
<select id="sasaran_id"
class="selectpicker form-control @error ('sasaran_id') is-invalid @enderror"
name="sasaran_id">
<option selected disabled>--silahkan pilih sasaran berikut--</option>
@foreach ($sasaran_id as $datasasaran)
<option value="{{ $datasasaran->id }}">{{$datasasaran->sasaran}}</option>
@endforeach
</select>
@error('sasaran_id')
<div class="invalid-feedback">
{{ $message }}
</div>
@enderror
</div>
<div id="indikatorSection">
<div class="form-row align-items-end ml-2 indikator">
<div class="form-group col-md-5">
<label for="indikator_id">Indikator</label>
<select id="indikator_id"
class="form-control @error ('indikator_id') is-invalid @enderror"
name="indikator_id[]">
<option selected disabled>--silahkan pilih indikator berikut--</option>
@foreach ($indikator_id as $dataindikator)
<option value="{{ $dataindikator->id }}">{{$dataindikator->indikator}}</option>
@endforeach
</select>
@error('indikator_id')
<div class="invalid-feedback">
{{ $message }}
</div>
@enderror
</div>
<div class="form-group col-md-1">
<label for="target_kinerja">Target</label>
<input type="number" name="target_kinerja[]"
class="form-control @error('target_kinerja') is-invalid @enderror"
id="target_kinerja" placeholder="" value="{{old('target_kinerja')}}">
@error('target_kinerja')
<div class="invalid-feedback">
{{ $message }}
</div>
@enderror
</div>
<div class="form-group col-md-1">
<button type="button" id="tambahIndikator" class="btn btn-primary"> Tambah </button>
</div>
</div>
</div>
js
<script>
$(document).ready(function () {
$('select[name=sasaran_id]').change(function() {
var url = '{{ url('sasaran') }}' + $(this).val() + '/indikator';
$.get(url, function(data) {
var select = $('form select[name=indikator_id[]]');
select.empty();
$.each(data,function(key, value) {
select.append('<option value=' + value.id + '>' + value.indikator + '</option>');
});
});
});
});
</script>
indikatorModel
public function sasaran()
{
return $this->belongsTo('App\ModelPerencanaan\SasaranModel');
}
sasaranModel
public function indikator()
{
return $this->hasMany('App\ModelPerencanaan\IndikatorModel','sasaran_id');
}
路线
Route::get('sasaran/{sasaran_id}/indikator', 'UnitKerja\Perencanaan\RabController@cariIndikator');
控制器
public function cariIndikator($sasaran_id)
{
$sasaran = SasaranModel::find($sasaran_id);
return $sasaran->indikator()->select('id', 'indikator')->get();
}
当我在路线上尝试url时,效果很好。.但看来我的js无效:/我想念什么吗?
答案 0 :(得分:0)
啊,我自己找到了答案。因此,基本上,在调试javascript之后,我发现我无法使用数组选择名称,因此,我需要输入的是[name =“ indikator []”]。然后代码更改如下:
<script>
$(document).ready(function () {
$('select[name=sasaran_id]').change(function() {
var url = '{{ url('sasaran') }}' + '/' + $(this).val() + '/indikator';
console.log(url);
$.get(url, function(data) {
var select = $('form select[name="indikator_id[]"]');
select.empty();
$.each(data,function(key, value) {
select.append('<option value=' + value.id + '>' + value.indikator + '</option>');
});
});
});
});
</script>