【问题标题】:get data on button click using jquery GET method使用jquery GET方法获取按钮点击数据
【发布时间】:2016-04-25 12:24:13
【问题描述】:

我已将 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>

我没有得到它在按钮单击时不起作用,而它在下拉更改事件中正常工作,请帮助。谢谢

【问题讨论】:

  • 考虑接受/关闭问题

标签: laravel


【解决方案1】:

您返回视图但需要一个 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);
});

【讨论】:

  • 我刚刚添加了 e.preventDefault();现在正在运行,谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-17
  • 2017-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多