【问题标题】:Simple calculation using script written in Google Sheets使用用 Google 表格编写的脚本进行简单计算
【发布时间】:2019-01-08 21:51:02
【问题描述】:

我对 html/js 中的计算非常陌生。一位同事给了我一个用 Google 表格编写的脚本,我正在尝试将它实现到我创建的 html 计算器中。这是我正在尝试做的一个简单示例(完整的脚本将在单击按钮时计算许多输入):

html:

<input type="text" id="A" value="2"> X
<input type="text" id="B" value="5">
<input type="button" onClick="calculate()" value="Calculate"> = 
<output id="total"></output>

脚本:

function calculate() {
    var A = document.getElementById("A").value;
    var B = document.getElementById("B").value;
    var total = A*B;
};

我知道这可能是非常错误的,但我正在学习:)

Codepen example

【问题讨论】:

    标签: javascript html calc


    【解决方案1】:

    您没有将计算的值分配给 DOM。

    工作示例:

    function calculate() {
        var A = document.getElementById("A").value;
        var B = document.getElementById("B").value;
        var total = A*B;
    
        document.getElementById("total").value = total;
    
    };
    <input type="text" id="A" value="2"> X
    <input type="text" id="B" value="5">
    <input type="button" onClick="calculate()" value="Calculate"> = 
    <output id="total"></output>

    【讨论】:

      猜你喜欢
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-21
      • 2020-10-29
      • 1970-01-01
      相关资源
      最近更新 更多