现有的我有动态依赖下拉选择输入系统。我正在使用laravel 5.6。这是我的控制器,
public function show($id)
{
$model_list = DB::table('brand')
->groupBy('c_id')
->get();
return view('cars.carform')->withModel_list($model_list);
}
function fetch1(Request $request)
{
$select = $request->get('select');
$value = $request->get('value');
$dependent = $request->get('dependent');
$data = DB::table('brand')
->where($select,$value)
->groupBy($dependent)
->get();
$output = '<option value="">Select '.ucfirst($dependent).'</option>';
foreach ($data as $row) {
$output .= '<option value="' .$row->$dependent. '">
'.$row->$dependent.'</option>';
}
echo $output;
}
这是选择输入的刀片文件,
<div class="form-group">
<label for="exampleFormControlSelect1">Vehicle Category</label>
<select name="c_id" id="c_id" class="form-control input dynamic" data-dependent="b_id" >
@foreach($model_list as $model)
<option value="{{$model->id}}">{{$model->id}}</option>
@endforeach
</select>
</div>
<div class="form-group">
<label for="exampleFormControlSelect1">Brand</label>
<select name="b_id" id="b_id" class="form-control input dynamic" data-dependent="na" >
<option value="">Select District</option>
</select>
</div>
<div class="form-group">
<label for="exampleFormControlSelect1">Model</label>
<select name="na" id="na" class="form-control input">
<option value="">Select Town</option>
</select>
</div>
jquery到刀片到下拉选择框,
<script>
$(document).ready(function(){
$('.dynamic').change(function(){
if($(this).val() != '')
{
var select = $(this).attr("id");
var value = $(this).val();
var dependent = $(this).data('dependent');
var _token = $('input[name="_token"]').val();
$.ajax({
url:"{{ route('cars.carform.fetch1')}}",
method:"POST",
data:{select:select, value:value, _token:
_token, dependent:dependent},
success:function(result)
{
$('#'+dependent).html(result);
}
})
}
});
});
</script>
在此系统上方,子选择框的值取决于第一个父选择框的值。但是现在我需要像这样将变量固定值保留给父选择输入,
<option value="{{$categories->id}}">{{$categories->id}}</option>
但是当我将此变量值放入选择输入值时,正在打印。但根据此值,子选择框不起作用。但是当我再次手动输入父级值时,子级选择有效。我需要自动选择父值,然后手动选择子vlues。我该怎么办?