在刀片视图中从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个回答

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



您有2个选项</ p>


  1. 将值作为ajax参数传递给 然后你需要改变路线和控制器</ li>
  2. 你建立网址并通过javascript追加id </ li>
    </ ol>

    url :“{{url('/')}} / {{$ store}} / {{$ table_name}} / {{$ receipt_id}} / shop / cart /”+ delete_id </ code> </ p> \ n </ div>

展开原文

原文

You have 2 options

  1. pass the value as a ajax param and then you will need to change the route and controller
  2. you build the url and append the id via javascript

url: "{{url('/')}}/{{$store}}/{{$table_name}}/{{$receipt_id}}/shop/cart/"+delete_id

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐