如果我启用/禁用了复选框按钮,则会从金额中删除已添加的.00。
如何将金额添加到.00
以下是我的代码:
function checks() {
document.getElementById('other_donation_check').checked = false;
}
function add(total, this_chk_bx) {
var thetotal = form2.thetotal.value;
if (this_chk_bx.checked == true) {
//add if its checked
var count=0;
form2.thetotal.value = Number(thetotal) + Number(total);
count++;
} else {
//subtract if its unchecked
form2.thetotal.value = thetotal - total;
}
}
我创建了一个fiddle。
我该如何解决这个问题?
答案 0 :(得分:0)
使用toFixed()
查找下面的代码段,以演示您要实现的目标;您可以使用实际逻辑扩展它。
(function add() {
document.getElementById('other_donation_check').addEventListener('click', function() {
var this_chk_bx = document.getElementById('other_donation_check').checked;
var amount_one = 2,
amount_two = 250;
if (this_chk_bx === true) {
console.log('is checked');
var thetotal = eval(amount_two + amount_one);
console.log(thetotal.toFixed(2));
} else {
console.log('is not checked');
var thetotal = eval(amount_two + amount_one);
console.log(thetotal);
}
});
}());
<p>Check / uncheck the checkbox below to see the code in action.</p>
<input type="checkbox" id="other_donation_check">