我有一個基于 HTML、JavaScript 和 PHP 構建的電子商務網站。在產品詳細信息頁面上,用戶可以將產品添加到購物車,因此我顯示的是購物車價值的總額。我想始終顯示十進制數 (10,2) 格式。目前我的代碼工作最少。如果產品價格為 12.00,則單擊“添加到購物車”時,計數器 div 僅顯示 12。<span><a href="cart.php" class="text-white"><i class="fa fa-shopping-cart p1" data-count="10" data-currency="€" id="total"></i></a></span> .p1[data-count]:after{ position:absolute; right:1%; top:1%; content: attr(data-count); font-size:10px; padding:.2em; line-height:1em; color: white; background:#21468b; text-align:center; width: 100%; font-weight:normal;}<script>var theTotal = '0';var ProductSellingPrice = '12.00'$('#insert').click(function(){ theTotal = Number(theTotal) + Number(ProductSellingPrice); $('#total').attr('data-count', theTotal);});</script>因此,在單擊插入時,將添加現有的 TheTotal 和當前產品價格。如果購物車上沒有產品,則 p1 不顯示任何值,因此如果為空/零,則希望始終顯示零。如果產品價格為 12.00,則僅顯示 12。如果產品價格為 12.50,則顯示 12.50。我希望它始終顯示十進制以及使用數據屬性的貨幣符號。顯示小數點問題由@Simone 解決,我無法找到使用數據屬性在值之前顯示貨幣的答案。
在一個 div 中顯示 2 個自定義數據屬性項。例如:貨幣和金額
慕婉清6462132
2021-11-25 15:55:27