我创建了ajax请求:
$('document').ready(function(){
$('#gettype button').click(function(){
var y = $(this).val();
$.get("{{url('gettypes')}}",{id:y}, function(data){
$('#samp').empty();
$.each(data, function(index, element) {
$('#samp').append("<option value='"+ element.ty_id +"'>" + element.ty_name + "</option>");
});
});
});
});
我希望在下拉列表或表格中点击按钮获取数据,我的路线是:
Route::get('gettypes',function(){
$catid = Request::get('id');
$typ = App\type::where('cat_id', $catid)->get();
return Response::make($typ);
});
我的刀片是:
<table class="table">
<tr id="gettype">
@foreach($category as $val)
<th><button class="btn btn-primary" value="{{$val->cat_id}}">{{$val->cat_name}}</button></th>
@endforeach
</tr>
</table >
和
<select id="samp" class="form-control">
<option value=""></option>
</select>
我没有得到它是不是按钮点击工作,它在下拉更改事件,plz帮助正确wirking。感谢
答案 0 :(得分:0)
你返回视图但需要一个json迭代foreach(jquery) 试试这个
Route::get('gettypes',function(){
$catid = Request::get('id');
$typ = App\type::where('cat_id', $catid)->get()->toArray(); //or toJson
return response()->json($typ);
});