简体   繁体   English

Ajax 增加购物车上的数量只能工作一次

[英]Ajax Increasing Quantity on Cart only Work Once

I have a page to increasing and decreasing quantity product on cart before going to checkout confirmation page.在进入结帐确认页面之前,我有一个页面可以增加和减少购物车上的产品数量。 Actually im doing with ajax, and then the back end will execute manipulate quantity of product based on button i clicked (it has product_id).实际上我是用ajax做的,然后后端将根据我点击的按钮(它有product_id)执行操作数量的产品。

The problem is, for the first time the ajax runs well and then i refresh the table (not whole page).问题是,第一次 ajax 运行良好,然后我刷新了表格(不是整个页面)。 But, when i click the button again.但是,当我再次单击按钮时。 It returns nothing.它什么都不返回。 BUT, after i refresh page using F5 and then click the button again, the quantity is updated.但是,在我使用 F5 刷新页面然后再次单击该按钮后,数量会更新。

Could you please show me the correct ways to solve this problem?你能告诉我解决这个问题的正确方法吗? (PS: Im sorry for my english) (PS:对不起我的英语)

Ajax call :阿贾克斯调用:

$(document).ready(function () {
    //Increase quantity on cart
    $(".btnPlus").on('click', function () {
        var id = $(this).val();
        var url = "CRUD member/update-checkout-plus.php";
        var postdata = {"id": id};
        $.post(url, postdata, function (html) {
            $("#myCart").load(location.href + " #myCart");
        });
    });

Here is the button, im only working for the button plus, the minus button i havent do that yet.这是按钮,我只为加号按钮工作,减号按钮我还没有这样做。

echo '<td style="text-align: center">'
        . '<button class="btnPlus" value="' . $item['id'] . '"><span class="glyphicon glyphicon-plus"></span></button>'
        . '<input type="text" class="fieldQty" value="' . $item['qty'] . '" style="text-align: center" size="2" readonly/>'
        . '<button class="btnMinus" value="' . $item['id'] . '"><span class="glyphicon glyphicon-minus"></span></button>'
        . '</td>';

Backend (update-checkout-plus.php) :后端(update-checkout-plus.php):

include '../../config.php';

$id = $_POST['id'];
$query = mysql_query("SELECT stock FROM products WHERE product_id = '$id'");
$row = mysql_fetch_array($query);
$stock = $row['stock'];

//if the quantity has reached maximum of stock (DB)
//quantity == $stock
//else quantity++
if ($_SESSION['cart'][$id]['qty'] >= $stock) {
    $_SESSION['cart'][$id]['qty'] = $stock;
} else {
    $_SESSION['cart'][$id]['qty'] ++;
}

What seems to me that you have a class .btn which resides in #mycart div, and every time you .load() you change the DOM so new elements gets in the div and that causes the old bindings gets removed from the DOM. 在我看来,你有一个类.btn驻留在#mycart div中,每次你.load()你改变DOM,所以新的元素进入div,这导致旧的绑定从DOM中删除。

So in this case you have to delegate the event to the closest static parent / document / body: 因此,在这种情况下,您必须将事件委托给最近的静态父/文档/正文:

$(document).on('click', '.btnPlus', function () {

Call this function,where you append .btnPlus 调用此函数,在其中追加.btnPlus

function bindAddToCart(){
        $(document).on('click', '.btnPlus', function () {
            //your click functionality
        });
    }

add location.reload() look at the code below:添加 location.reload() 看下面的代码:

$('.cart_quantity_up').click(function(){
    var id=$(this).attr("pid").toString();
    var pls=this.parentNode.children[1]
    console.log(pls)
    console.log(id)
    $.ajax({
        type:"GET",
        url:"/pluscart/",
        data:{
            prod_id:id
        },
        success:function(data){
            pls.innerText=data.quantity
            location.reload()
        }


    })
});

Go to YourTheme/templates/checkout/cart.phtml and paste the below code: 转到YourTheme / templates / checkout / cart.phtml并粘贴以下代码:

<script type="text/javascript">
function changeItemQuantity( qty,num,cartid) {        
    var num = num;
    var cartid = cartid;
    var quantity = document.getElementById(cartid).value
    /* Restrict Quantity as a Non Negative */ 
    quantity = Math.max(1, quantity);

    var currentVal = parseInt(quantity);
    var final_val = currentVal + num;
    document.getElementById(cartid).value=final_val;
}    
</script>

Go to app/design/frontend/YourTheme/default/template/checkout/cart/item/default.phtml and paste this code around line 207: 转到app / design / frontend / YourTheme / default / template / checkout / cart / item / default.phtml并在第207行粘贴此代码:

<a class="mobile-only" onclick="changeItemQuantity(<?php echo $this->getQty() ?>,-1,<?php echo $_item->getId()?>); return false;" href="#"> - </a>

    <label class="mobile-only m-text"><?php echo $this->__('QTY') ?></label>  

        <input name="cart[<?php echo $_item->getId() ?>][qty]" value="<?php echo $this->getQty() ?>" size="4" title="<?php echo $this->__('Qty') ?>" id="<?php echo $_item->getId()?>" class="input-text qty" maxlength="12" />

     <a class="mobile-only" onclick="changeItemQuantity(<?php echo $this->getQty() ?>,1,<?php echo $_item->getId()?>); return false;" href="#"> + </a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 jQuery 和 AJAX 只工作一次 - jQuery and AJAX work only once jQuery Ajax调用似乎只工作一次? - jquery Ajax call seems to work only once? 如何使用ajax,jquery,php和session更新购物车数量 - How to update cart quantity using ajax,jquery,php and sessions ajax提交停留在同一页上+更新购物车中产品的数量 - ajax submit stay on same page + update quantity of product in cart 在 Shopify 商店中使用 AJAX 将所有产品变体和数量添加到购物车 - Add all product variant and quantity to the cart with AJAX in Shopify store 添加到购物车功能的其他部分也是将新数量放入 cartItems 数组,而不仅仅是增加商品数量 - My else part of add to cart functionality is also putting the new quantity in the cartItems array rather than just increasing quantity of Items 将多个相同商品添加到非常简单的购物车元素时,增加商品数量时遇到麻烦 - Having trouble increasing item quantity when multiple of the same item are added to very simple cart element Javascript-仅通过一次按键即可增加价值 - Javascript - Increasing value by keypress, only once 仅使用第一个数量的Ajax更新数量 - Update quantity with ajax only first one quantity is working Redux: addToCart function 只是更新数量而不是将产品添加到购物车 - Redux: addToCart function is only updating quantity and not adding the product to cart
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM