【问题标题】:Laravel Ajax subtotal only changing for one product, and not working for multiple productsLaravel Ajax 小计仅针对一种产品更改,不适用于多种产品
【发布时间】:2021-02-16 13:17:08
【问题描述】:

这是一个购物车,数量乘以价格显示小计,当我只在购物车中输入一个项目时,它可以工作,但是当我输入多个项目时,它会出现此错误。 完整性约束违规:1048 列“数量”不能为空(SQL:更新carts 设置quantity = ?)”

我相信它来自这个 $("#qty").change(function(e) Id 采取了一项,但我不确定,我们将非常感谢您的帮助

刀片:

<tr>
    <td class="product-thumbnail">
        <a href="#"><img style="object-fit: contain;" src="{{asset('product-images/'.$product->image)}}" alt=""  width="200px" height="120px"></a>
    </td>
    <td class="product-name">{{$product->title}}</td>
    <td class="product-price-cart"><span class="amount" id="price">{{$product->price}} </span> $</td>
    <td class="product-quantity ">
        <div class="cart-plus-minus ">
            <input class="cart-plus-minus-box qty" type="number" id="qty" class="qty" name="qty" value="1" >
        </div>
    </td>
    <td class="product-subtotal" id="subtotal"><span class="subtotal">{{$product->price}} </span>$ </td>

    <td>
    <form action="{{route('cart.destroy',$cart->id)}}" method="POST" >
        @csrf
    @method('DELETE')
    <button class="btn btn-danger">Delete</button>

    </form>

</td>
</tr>

刀片中的脚本:

$( document ).ready(function() {
    $("#qty").change(function(e){
        
e.preventDefault();
var selct_ = $(this) //declare this
$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});
    $.ajax({
    url: "{{ route('checkout.qty') }}", 
    data:{
    'quantity':qty.value
    }, 
    type: "get",
    success: function(result){
     var price=selct_.closest('tr').find('.amount').text();
     console.log(price);
     $('.subtotal').text(result[0]*price);
    }
  });
});
});

控制器

public  function quantity(Request $request){
DB::table('carts')->update(
['quantity'=> $request->quantity]
);
return response()->json([$request->quantity,200]); 
}

【问题讨论】:

    标签: ajax laravel


    【解决方案1】:

    数量未定义。试试

    $( document ).ready(function() {
        $("#qty").change(function(e){
    
            e.preventDefault();
            var qty = e.target;
    
    
            var selct_ = $(this) //declare this
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });
                $.ajax({
                url: "{{ route('checkout.qty') }}", 
                data:{
                        'quantity':qty.value
                }, 
                type: "get",
                success: function(result){
                var price=selct_.closest('tr').find('.amount').text();
                console.log(price);
                $('.subtotal').text(result[0]*price);
               }
            });
          });
       });
    
    
    

    【讨论】:

    • 现在当我有 2 个产品,并更改第一个产品的数量时,第一个和第二个项目的小计正在改变,$('.subtotal').text(result[0]*价钱);也许应该改变?
    • 是的,使用 "$('.subtotal').text(result[0]*price);"您正在更新所有具有“.subtotal”类的项目
    猜你喜欢
    • 2023-01-25
    • 2010-10-16
    • 2017-03-12
    • 1970-01-01
    • 1970-01-01
    • 2021-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多