dongli8466 2018-08-13 08:21
浏览 79
已采纳

在刀片视图中从jquery变量传递路由参数

How to add the delete_id = $(this).attr('delete_id'); jquery variable in the 'product_id' => ???? of the ajax url

I have this route: Route::delete('/{store}/{table_name}_{table_id}/{receipt_id}/shop/cart/{product_id}', 'Cart\CartController@destroy')->name('cart.destroy');

and this ajax:

 $(document).on('submit','#cartItem_delete',function(e){
        delete_id = $(this).attr('delete_id');
         var form_data = $(this).serialize(); //prepare form data for Ajax post
        $.ajax({
          type: "DELETE",
          url: '{{ route('cart.destroy', ['product_id' => ????, 'store' => $store, 'table_name' => $table_name, 'table_id' => $table_id, 'receipt_id' => $receipt_id]) }}',
          dataType:"json", //expect json value from server
          data: form_data
        }).done(function(data){ //on Ajax success    
                $('tr[row='+delete_id+']').remove();
                $('.cart-button a span').text(data.count);
                if (data.count < 1) {
                    $('.container #cartContainer').remove();
                    location.reload();
                }
        });
        e.preventDefault();
    });

The html form is in a foreach statement (for each row in the cart) and the row id is in the delete_id attribute:

<form id="cartItem_delete" delete_id="{{ $item->rowId }}" class="side-by-side">
                                {!! csrf_field() !!}
                                <input type="hidden" name="_method" value="DELETE">
                                <button type="submit" class="btn btn-danger btn-sm" value="Remove">Remove</button>
                            </form>
  • 写回答

2条回答 默认 最新

  • dtot74529 2018-08-13 08:41
    关注

    You can add action attribute to form with url:

    <form action="{{ route('cart.destroy', ['product_id' => $item->rowId, 'store' => $store, 'table_name' => $table_name, 'table_id' => $table_id, 'receipt_id' => $receipt_id]) }}"...
    

    then get url in submit event:

    var action_url = $( this ).attr('action')
    

    and pass it to ajax:

    $.ajax({
        url: action_url
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?