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

时间:2018-08-13 08:21:03

标签: php jquery ajax laravel laravel-blade

如何在ajax delete_id = $(this).attr('delete_id');的{​​{1}}中添加'product_id' => ???? jquery变量

我有这条路线: url

和这个ajax:

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

html表单位于 $(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(); }); 语句中(对于购物车中的每一行),行ID位于foreach属性中:

delete_id

2 个答案:

答案 0 :(得分:0)

您有2个选项

  1. 将值作为ajax参数传递,然后您需要更改路由和控制器
  2. 您构建网址并通过javascript附加ID

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

答案 1 :(得分:0)

您可以将动作属性添加到带有url的表单:

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

然后在提交事件中获取网址:

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

并将其传递给ajax:

$.ajax({
    url: action_url