使用laravel 5.6,在我的应用程序中,我具有动态依赖的下拉选择框。这是我的控制器,
public function show($id)
{
$categories = Category::find($id);
$catagory_list = DB::table('models')
->groupBy('catagory_id')
->get();
$model_list = DB::table('brand')
->groupBy('c_id')
->get();
return view('cars.carform')->withCategories($categories)->withCatagory_list($catagory_list)->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="{{$categories->id}}">{{$categories->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>
但是我可以在输入框中看到值。 ($ categories-> id),但由于其他依赖选择框不起作用。当我再次手动选择值时,它正在工作。我需要自动选择$ categories-> id值,并动态依赖于其他选择框。