为了账号安全,请及时绑定邮箱和手机立即绑定

如何在结帐总额的末尾添加零?

如何在结帐总额的末尾添加零?

慕尼黑5688855 2023-09-18 15:19:37
我想在结帐总金额上有一个零,因为在末端以零结尾的产品上,总金额显示为这样 £1.5那么,如何更改它以像这样显示1.50英镑而不会弄乱其他产品呢?任何代码片段都将不胜感激。<!DOCTYPE html><html><head>   </head><body><header class="main-header">    <nav class="main-nav nav">    </nav></header>    <div class="shop-items">        <div class="shop-item">            <span class="shop-item-title">Album 0</span>&nbsp;            <div class="shop-item-details"><span class="shop-item-price">£12.50</span>            <button class="btn btn-primary shop-item-button" type="button">ADD TO BASKET</button>        </div>  <div class="shop-item">        <span class="shop-item-title">Album 2</span>&nbsp;        <div class="shop-item-details">            <span class="shop-item-price">£14.50</span>                       <button class="btn btn-primary shop-item-button" type="button">ADD TO BASKET</button>        </div>        <div class="shop-item">        <span class="shop-item-title">Album 4</span>&nbsp;        <div class="shop-item-details"><span class="shop-item-price">£19.50</span>        <button class="btn btn-primary shop-item-button" type="button">ADD TO BASKET</button>    </div>        </div></section>    <div class="shop-items">        <div class="shop-item">            <span class="shop-item-title">Album 6</span>&nbsp;            <div class="shop-item-details"><span class="shop-item-price">£19.50</span>            <button class="btn btn-primary shop-item-button" type="button">ADD TO BASKET</button>        </div>     <div class="shop-items">        <div class="shop-item">            <span class="shop-item-title">Album 8</span>&nbsp;            <div class="shop-item-details"><span class="shop-item-price">£19.50</span>            <button class="btn btn-primary shop-item-button" type="button">ADD TO BASKET</button>        </div>   
查看完整描述

1 回答

?
慕森卡

TA贡献1806条经验 获得超8个赞

在代码末尾,将此代码编辑为totaltotal.toFixed(2)


编辑:


在代码的最后一行


function updateCartTotal() {

var cartItemContainer = document.getElementsByClassName('cart-items')[0]

var cartRows = cartItemContainer.getElementsByClassName('cart-row')

var total = 0

for (var i = 0; i < cartRows.length; i++) {

    var cartRow = cartRows[i]

    var priceElement = cartRow.getElementsByClassName('cart-price')[0]

    var quantityElement = cartRow.getElementsByClassName('cart-quantity-input')[0]

    var price = parseFloat(priceElement.innerText.replace('£', ''))

    var quantity = quantityElement.value

    total = total + (price * quantity)

}

total = Math.round(total * 100) / 100

document.getElementsByClassName('cart-total-price')[0].innerText = '£' + total.toFixed(2)

}


查看完整回答
反对 回复 2023-09-18
  • 1 回答
  • 0 关注
  • 81 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信