我有一个价值 1,500.85 的产品,我想将它与使用 javascript 输入的数量相乘。但是因为金额超过一千,所以有一个逗号,并且由于那个逗号,结果显示为“NaN”..我如何用逗号计算金额?
注意:如果金额低于一千,它会起作用。
// Calculation script START
$(document).ready(function() {
CalculateTotalPrice();
});
document.getElementsByClassName("input-text qty text")[0].onkeyup = function() {CalculateTotalPrice()};
$(select).onchange(function() {
CalculateTotalPrice();
});
function CalculateTotalPrice() {
setTimeout(function(){
var price = document.querySelector(".price .woocommerce-Price-amount.amount").innerText;
var quantity = document.getElementsByClassName("input-text qty text")[0].value;
var total = price * quantity;
var totalOnly2Decimal = total.toFixed(2);
document.getElementById("result").innerHTML = "DKK " + totalOnly2Decimal + " inkl. moms";
}, 100);
}
// Calculation script END
<!-- Price -->
<div class="elementor-widget-container">
<p class="price">Fra:
<span class="woocommerce-Price-amount amount">
<span class="woocommerce-Price-currencySymbol"></span>1,122.50
</span> kr. inkl. moms</p>
</div>
<!-- Quantity field -->
<div class="quantity">
<label class="screen-reader-text" for="quantity_5cd3fab7bb0d7"></label>
<input type="number" id="quantity_5cd3fab7bb0d7" class="input-text qty text" step="1" min="1" max="" name="quantity" value="1" title="Stk." size="4" inputmode="">
<!-- Result -->
<h3 style="font-size: 17px; font-weight:bold; display:inline; text-transform:uppercase;">Total:</h3>
<p class="result" id="result" style="display:inline;"></p>
将此行更改为以下内容
var price =
document.querySelector(".price .woocommerce-Price-amount.amount").innerText.split(",").join("");
// Calculation script START
$(document).ready(function() {
CalculateTotalPrice();
});
document.getElementsByClassName("input-text qty text")[0].onkeyup = function() {
CalculateTotalPrice()
};
function CalculateTotalPrice() {
setTimeout(function() {
var price = document.querySelector(".price .woocommerce-Price-amount.amount").innerText.split(",").join("");
var quantity = document.getElementsByClassName("input-text qty text")[0].value;
var total = price * quantity;
var totalOnly2Decimal = total.toFixed(2);
document.getElementById("result").innerHTML = "DKK " + totalOnly2Decimal + " inkl. moms";
}, 100);
}
// Calculation script END
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- Price -->
<div class="elementor-widget-container">
<p class="price">Fra:
<span class="woocommerce-Price-amount amount">
<span class="woocommerce-Price-currencySymbol"></span>1,122.50
</span> kr. inkl. moms</p>
</div>
<!-- Quantity field -->
<div class="quantity">
<label class="screen-reader-text" for="quantity_5cd3fab7bb0d7"></label>
<input type="number" id="quantity_5cd3fab7bb0d7" class="input-text qty text" step="1" min="1" max="" name="quantity" value="1" title="Stk." size="4" inputmode="">
<!-- Result -->
<h3 style="font-size: 17px; font-weight:bold; display:inline; text-transform:uppercase;">Total:</h3>
<p class="result" id="result" style="display:inline;"></p>
<script>
</script>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句