如何使用复选框计算保持 .00



如果我启用/禁用了复选框按钮,则添加的.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;
    }
}

我创造了一个小提琴。

如何解决此问题?

在下面找到一个片段,使用 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">

最新更新