首页 > 解决方案 > 更改数量 javascript

问题描述

我有一个 javascript 代码可以更改 Magento 中产品的数量,但我有一个问题。当我减少数量时,最新值为 0 的值为 -1。如何停止减少按钮以仅保留在 0 值上?

谢谢

     /********************* Qty Holder **************************/
    $(document).on("click", ".qtyplus", function(e) {
        // Stop acting like a button
        e.preventDefault();
        // Get its current value
        var currentVal = parseInt($(this).parents('.qty-changer').find('input.input-text.qty').val());

        // If is not undefined
        if (!isNaN(currentVal)) {
            // Increment
            $(this).parents('.qty-changer').find('input.input-text.qty').val(currentVal + 1);
            $(this).parents('.qty-changer').find('.update-cart-item').show();
        } else {
            // Otherwise put a 0 there
            $(this).parents('.qty-changer').find('input.input-text.qty').val(1);
        }
    });
    // This button will decrement the value till 0
    $(document).on("click", ".qtyminus", function(e) {
        // Stop acting like a button
        e.preventDefault();
        // Get the field name
        fieldName = $(this).attr('field');
        // Get its current value
        var currentVal = parseInt($(this).parents('.qty-changer').find('input.input-text.qty').val());

        // If it isn't undefined or its greater than 0

        if (!isNaN(currentVal) && currentVal > 1) {
            // Decrement one
            $(this).parents('.qty-changer').find('input.input-text.qty').val(currentVal - 1);
            $(this).parents('.qty-changer').find('.update-cart-item').show();
        } else {
            // Otherwise put a 0 there
            $(this).parents('.qty-changer').find('input.input-text.qty').val(1);
        }
    }); 

标签: javascriptphpjquerymagentomagento2

解决方案


推荐阅读