我必须经常这样做,这是我的方式
HTML/刀片
<select name="select_name" id="select1" class="form-control">
<option value="">choose</option
@foreach($models as $model)
<option value="{{ $model->id }}">{{ $model->attribute }}</option>
@endforeach
</select>
<select name="select_name" id="select2" class="form-control">
<option value="">choose select 1 first</option>
</select>
$("#select1").change(function(e){
var model = e.target.value;
$.get("/api/your-route?input="+model, function(data){
$("#select2").empty();
$("#select2").append("<option value=''>choose</option>");
$.each(data, function(index, obj){
$("#select2").append("<option value='model.id'>model.attribute</option>");
});
});
});
拉瓦维尔
public function func_name(Request $request){
//if you work with Laravel 5.8 or older, you could you the input facade
$childern = Child::where('parent_id', $request->input('parent'))->get();
return response()->json($childern, 200);
// if you want to use a resource, that's even better
}
它对我来说一直都是一种魅力,因为每次父对象发生变化时,我都会从api中获取值,所以我不必担心有人会利用检查器中的值