Javascript代码未正确使用Google Apps和HTML计算数字总和

jucafojl  于 2023-02-02  发布在  Java
关注(0)|答案(1)|浏览(114)

我在Google Apps中编写了一个JavaScript,它将把HTML用户输入的每月美元写入电子表格。
它的功能是通过html输入从用户那里获取输入。然后我使用一个类来捕获和计算用户输入的内容,这些内容是主动写入屏幕的。
它需要接受正数和负数,虽然它接受用户的输入,但它只显示数字为正数,即使是负数,另外它计算不正确,我注意到如果数字有逗号,例如1000,它删除了逗号后面的所有内容。2我在标题下面分享脚本,然后在HTML文件的主体中分享脚本,以及带有函数的HTML代码。3最后,我使用的是Bootstrap,它在代码的其他区域运行良好。
标题脚本

<script>
 
  function toFinalNumberFormat(controlToCheck) {
    var enteredNum = '' + controlToCheck.value;
    enteredNum = enteredNum.replace(/[^0-9\.]+/g, ''); 
    controlToCheck.value = Number(enteredNum).toLocaleString();
  }
</script>

HTML正文中的脚本

document.getElementsByClassName("add-input").addEventListener("input", sumTotal);  //listener when numbers are inputted into monthly spend fields

 function sumTotal ()  {
    var inputs = document.getElementsByClassName('add-input')

    var totalSum = 0

    for ( let i=0; i < inputs.length; i++) {
      totalSum += parseFloat(inputs[i].value || 0, 10)
    }
     var total = document.getElementById("total");
     total.value = totalSum.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,');
      
  }

这是HTML代码段

<h5>Monthly Spend</h5>
 <div class="container3">
   <div class = "row">
      <div class="form-floating col-sm-2"> 
        <input type="text" id="jan" class="form-control input-group add-input" aria-placeholder="January" onblur="toFinalNumberFormat(this);" oninput="sumTotal()" placeholder="0.00">
        <label for="jan">January<br>$</label>
      </div>

      <div class="form-floating col-sm-2"> 
        <input type="text" id="feb" class="form-control input-group add-input" aria-placeholder="February" onblur="toFinalNumberFormat(this);" oninput="sumTotal()" placeholder="0.00">
        <label for="feb">February<br>$</label>
      </div>

       <div class="form-floating col-sm-2"> 
        <input type="text" id="mar" class="form-control input-group add-input" aria-placeholder="March" onblur="toFinalNumberFormat(this);" oninput="sumTotal()" placeholder="0.00">
        <label for="mar">March<br>$</label>
      </div>

     <div class="form-floating col-sm-2"> 
        <input type="text" id="apr" class="form-control input-group add-input" aria-placeholder="April" onblur="toFinalNumberFormat(this);" oninput="sumTotal()" placeholder="0.00">
        <label for="apr">April<br>$</label>
      </div>

      <div class="form-floating col-sm-2"> 
        <input type="text" id="may" class="form-control input-group add-input" aria-placeholder="May" onblur="toFinalNumberFormat(this);" oninput="sumTotal()" placeholder="0.00">
        <label for="may">May<br>$</label>
      </div>

      <div class="form-floating col-sm-2"> 
        <input type="text" id="jun" class="form-control input-group add-input" aria-placeholder="June" onblur="toFinalNumberFormat(this);" oninput="sumTotal()" placeholder="0.00">
        <label for="jun">June<br>$</label>
      </div>
  </div>

  <div class = "row">
      <div class="form-floating col-sm-2"> 
        <input type="text" id="jul" class="form-control input-group add-input" aria-placeholder="July" onblur="toFinalNumberFormat(this);" oninput="sumTotal()" placeholder="0.00">
        <label for="jul">July<br>$</label>
      </div>

      <div class="form-floating col-sm-2"> 
        <input type="text" id="aug" class="form-control input-group add-input" aria-placeholder="August" onblur="toFinalNumberFormat(this);" oninput="sumTotal()" placeholder="0.00">
        <label for="aug">August<br>$</label>
      </div>

      <div class="form-floating col-sm-2"> 
        <input type="text" id="sep" class="form-control input-group add-input" aria-placeholder="September" onblur="toFinalNumberFormat(this);" oninput="sumTotal()" placeholder="0.00">
        <label for="sep">September<br>$</label>
      </div>

      <div class="form-floating col-sm-2"> 
        <input type="text" id="oct" class="form-control input-group add-input" aria-placeholder="October" onblur="toFinalNumberFormat(this);" oninput="sumTotal()" placeholder="$0.00">
        <label for="oct">October<br>$</label>
      </div>

      <div class="form-floating col-sm-2"> 
        <input type="text" id="nov" class="form-control input-group add-input" aria-placeholder="November" onblur="toFinalNumberFormat(this);" oninput="sumTotal()" placeholder="0.00">
        <label for="nov">November<br>$</label>
      </div>

      <div class="form-floating col-sm-2"> 
        <input type="text" id="dec" class="form-control input-group add-input" aria-placeholder="December" onblur="toFinalNumberFormat(this);" oninput="sumTotal()" placeholder="0.00">
        <label for="dec">December<br>$</label>
      </div>
</div>
  <div class = "row">
     
    <div class="form-floating col-sm-4"> 
        <h3>Total:  $<output type="number" id="total"></output></h3>
        
      </div>

有人能指出代码的错误吗?谢谢!

06odsfpq

06odsfpq1#

document.getElementsByClassName()返回一个元素列表,您需要对其进行迭代以添加所有事件侦听器。
您需要删除循环中每个值上调用parseFloat()的逗号,而不是在计算总数之后,因为parseFloat()无法解析带有逗号分隔符的数字。

document.querySelectorAll(".add-input").forEach(input => {
  input.addEventListener("input", sumTotal);
  input.addEventListener("blur", () => toFinalNumberFormat(input));
});

function sumTotal() {
  var inputs = document.getElementsByClassName('add-input');

  var totalSum = 0

  for (let i = 0; i < inputs.length; i++) {
    totalSum += parseFloat(inputs[i].value.replace(/[^-\d.]/g, '') || 0);
  }
  var total = document.getElementById("total");
  total.value = totalSum.toFixed(2);
}

function toFinalNumberFormat(controlToCheck) {
  var enteredNum = '' + controlToCheck.value;
  enteredNum = enteredNum.replace(/[^-\d.]/g, '');
  controlToCheck.value = Number(enteredNum).toLocaleString();
}
<h5>Monthly Spend</h5>
<div class="container3">
  <div class="row">
    <div class="form-floating col-sm-2">
      <input type="text" id="jan" class="form-control input-group add-input" aria-placeholder="January" placeholder="0.00">
      <label for="jan">January<br>$</label>
    </div>

    <div class="form-floating col-sm-2">
      <input type="text" id="feb" class="form-control input-group add-input" aria-placeholder="February" placeholder="0.00">
      <label for="feb">February<br>$</label>
    </div>

    <div class="form-floating col-sm-2">
      <input type="text" id="mar" class="form-control input-group add-input" aria-placeholder="March" placeholder="0.00">
      <label for="mar">March<br>$</label>
    </div>

    <div class="form-floating col-sm-2">
      <input type="text" id="apr" class="form-control input-group add-input" aria-placeholder="April" placeholder="0.00">
      <label for="apr">April<br>$</label>
    </div>

    <div class="form-floating col-sm-2">
      <input type="text" id="may" class="form-control input-group add-input" aria-placeholder="May" placeholder="0.00">
      <label for="may">May<br>$</label>
    </div>

    <div class="form-floating col-sm-2">
      <input type="text" id="jun" class="form-control input-group add-input" aria-placeholder="June" placeholder="0.00">
      <label for="jun">June<br>$</label>
    </div>
  </div>

  <div class="row">
    <div class="form-floating col-sm-2">
      <input type="text" id="jul" class="form-control input-group add-input" aria-placeholder="July" placeholder="0.00">
      <label for="jul">July<br>$</label>
    </div>

    <div class="form-floating col-sm-2">
      <input type="text" id="aug" class="form-control input-group add-input" aria-placeholder="August" placeholder="0.00">
      <label for="aug">August<br>$</label>
    </div>

    <div class="form-floating col-sm-2">
      <input type="text" id="sep" class="form-control input-group add-input" aria-placeholder="September" placeholder="0.00">
      <label for="sep">September<br>$</label>
    </div>

    <div class="form-floating col-sm-2">
      <input type="text" id="oct" class="form-control input-group add-input" aria-placeholder="October" placeholder="$0.00">
      <label for="oct">October<br>$</label>
    </div>

    <div class="form-floating col-sm-2">
      <input type="text" id="nov" class="form-control input-group add-input" aria-placeholder="November" placeholder="0.00">
      <label for="nov">November<br>$</label>
    </div>

    <div class="form-floating col-sm-2">
      <input type="text" id="dec" class="form-control input-group add-input" aria-placeholder="December" placeholder="0.00">
      <label for="dec">December<br>$</label>
    </div>
  </div>
  <div class="row">

    <div class="form-floating col-sm-4">
      <h3>Total: $<output type="number" id="total"></output></h3>

    </div>

相关问题