ajax onlick按钮仅在第二次单击时有效

时间:2020-06-28 22:31:36

标签: jquery ajax laravel

我制作了一个按钮,可以增加产品的数量,同时增加产品的尺寸,效果很好。但是我有一个ajax请求,该请求可以计算产品的价格并为运输增加价值,但是由于某种原因,它仅在第二次单击中起作用,而忽略了第一次单击和计算。

我的html页面:

在我的控制器中,我有一个@if表示,如果价格超过30欧元,它将更改portes的值,并且仅在第二次单击后更改。

<li id="portes"><span><strong> Portes: </strong></span><span>@if(Cart::getSubTotal() ==
                                0){{ 0 . ' €' }}@else{{ number_format($envioPortes, 2)  . ' €' }}@endif</span>
                        </li>

我的main.js:

function plusqty(id) {

let qty = $('#qty' + id).val();

$('#qty' + id).val(parseInt(qty) + 1);

let qtyupdated = $('#qty' + id).val();

$.ajax({
    url: "/cart-update",
    type: "POST",
    data: {
        id: id,
        quantity: qtyupdated,
        'cobranca': $('#cobranca_check').prop("checked")
    },
    async: true,
    success: function (response) {

        Swal.fire({
            title: "Quantidade atualizada com sucesso",
            background: '#f74877',
            position: 'center',
            toast: true,
            allowEscapeKey: false,
            showCloseButton: false,
            showConfirmButton: false,
            timer: 1000,
        });

        $('.cart_holder').html(response.cart);
        $('.modalQty' + id).html($('#qty' + id).val());
        $("#modalSubTotal").text(response.subTotalModal);
    },
    error: function (xhr, textStatus, errorThrown) {}
});

}

我的3个按钮,我可以手动添加和添加一些数量:

<td class="column-4">
                        <div class="wrap-num-product flex-w m-l-auto m-r-0">
                            <button type="button" class="btn-num-product-down cl8 trans-04 flex-c-m qtyA"
                                onclick="minusqty({{ $product->id }})">
                                <i class="fs-16 zmdi zmdi-minus"></i>
                            </button>

                            <input class="mtext-104 cl3 txt-center num-product quantity-amount{{$product->id}}"
                                type="text" name="num-product" oninput="manualqty({{ $product->id }})"
                                value="{{ $product->quantity }}">

                            <button type="button" class="btn-num-product-up cl8 trans-04 flex-c-m qtyA"
                                onclick="plusqty({{ $product->id }})">
                                <i class="fs-16 zmdi zmdi-plus"></i>
                            </button>
                            <input type="hidden" class="hidden" id="qty{{$product->id}}"
                                value="{{ $product->quantity }}">
                        </div>

谢谢!

0 个答案:

没有答案