问题描述
我想使用减号将数字显示为负数:5000->-5000
<div class="cart-summary-line" id="cart-subtotal-discount">
<span class="label">
Remise
</span>
<span class="value">5,000 TND</span>
</div>
$(document).ready(function() {
return "-" + ($(['#cart-subtotal-discount'.value]).html(5.000));
});
解决方法
您的JavaScript和jQuery语法有很多问题。我建议您重新阅读jQuery documentation and examples。
$(document).ready(function() {
return "-"+($(['#cart-subtotal-discount'.value]).html(5.000));
// ^ we don't need the return value
// ^ concatenating a "-" here won't set anything
// ^ don't need the extra parentheses here
// ^ why square brackets? this isn't an array
// ^ this selector contains a lot more text than just the number
// ^ do we want `value` or `html`?
// ^ are we getting,setting,or both?
// ^ `5.000` means `5` in JS,not `5000`
});
这是您可能实现的目标的一种方法:
const positiveNumber = 5000
const negativeNumber = 0 - positiveNumber
const locale = 'ar-TN' // Tunisian locale,assumed from "TND" currency
// Using `toLocaleString` with the ar-TN locale will give you
// the "5.000" formatting you want,but you still need to
// write the number as `5000` in JavaScript.
const formattedNumber = negativeNumber.toLocaleString(locale)
$(document).ready(function() {
$('#amount').html(formattedNumber)
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>Remise <span id="amount"></span> TND</p>