购物车总价格和数量

时间:2019-03-24 22:46:21

标签: javascript

下面有一个购物车,当您删除产品时,它会起作用并且价格会更新,但是当我尝试添加更多数量(如2或3)时,总价不会更新,但是当我删除产品时,它将得到更新更新,这就是我不知道如何解决的问题所在。 我还添加了一个带有数量的函数,因此最小订单数只能是1,如果您尝试添加少于1的数字(例如0或-1,-2),它将自动更改为数字1,但是不起作用,我需要一些帮助。

p.s:这只是一个练习,因此没有PHP或其他任何东西。

int main() {

  int size = 0;
  int out_size = 0;
  int arr[size];

  printf("Enter the length of the array: ");
  scanf("%d", & size);
  printf("\nEnter the elements of the array:  ");
  for (int i = 0; i < size; i++) {
    scanf("%d", & arr[i]);
  }

  if (size % 2 == 0) {
      out_size = size/2;
  }
  else{
      out_size = ((size-1)/2) + 1;
  }

  int out[out_size];

  //calling the add function and using the addresses of arrays + array size
  add(arr, size, out);

  //iterating through and printing output array which has now been
  //altered by the move function
  printf("\nThe output array is: ");
  for (int i = 0; i < out_size; i++) {
    printf("%d ", out[i]);
  }
  return 0;
}

1 个答案:

答案 0 :(得分:0)

    <!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" >
        <div>
            <div class="click-cart">
                <div class="cart-row2">
                    <span class="cart-item cart-header cart-column">ITEM</span>
                    <span class="cart-price cart-header cart-column">PRICE</span>
                    <span class="cart-quantity cart-header cart-column">QUANTITY</span>
                </div>
                <div class="cart-row">
                    <div class="lakn">
                        <img class="shop-item-cart" src="https://www.gettyimages.ca/gi-resources/images/Homepage/Hero/UK/CMS_Creative_164657191_Kingfisher.jpg">
                        <span class="shop-title">Product 1</span>
                        <span class="shop-price">$19.99</span>
                        <input class="quantity-input" type="number" value="1" />
                        <button class="delete-cart">X</button>
                    </div>
                </div>
                <div class="clear-checkout">
                    <button class="checkout">Checkout</button>
                    <button class="clear-cart">Clear Cart</button>
                </div>
                <div class="cart-total">
                    <strong class="cart-total-title">Total</strong>
                    <span class="cart-total-price">$10.79</span>
                </div>
            </div>
        </div>
    </form>
    <script>
        let removeCartItemButtons = document.querySelectorAll('.delete-cart');
        for (let i = 0; i < removeCartItemButtons.length; i++) {

            let button = removeCartItemButtons[i]
            button.addEventListener('click', removeCartItem)
        }

        let quantityInputs = document.querySelectorAll('.quantity-input');
        for (let i = 0; i < quantityInputs.length; i++) {

            let input = quantityInputs[i]
            input.addEventListener('change', quantityChanged);
        }

        function removeCartItem(event) {
            let buttonCliked = event.target;
            buttonCliked.parentElement.parentElement.remove()
            updateCartTotal()
        }

        function quantityChanged() {

            let input = event.target;
            if (isNaN(input.value) || input.value <= 0) {
                input.value = 1;
            }
            updateCartTotal()
        }

        function updateCartTotal() {
            let cartItemContainer = document.querySelector('.click-cart');
            let cartRows = cartItemContainer.querySelectorAll('.cart-row');

            let total = 0
            for (let i = 0; i < cartRows.length; i++) {
                let cartRow = cartRows[i]
                let priceElement = cartRow.querySelector('.shop-price');
                let quantityElement = cartRow.querySelector('.quantity-input');

                let price = parseFloat(priceElement.innerText.replace('$', ''))
                let quantity = quantityElement.value
                total = total + (price * quantity)
            }
            total = Math.round(total * 100) / 100
            document.querySelector('.cart-total-price').innerText = '$' + total;
        }
    </script>
</body>
</html>