【问题标题】:Javascript: Multiplying an input value with multiple table cell values and assign it to other table cellsJavascript:将输入值与多个表格单元格值相乘并将其分配给其他表格单元格
【发布时间】:2016-10-28 09:25:30
【问题描述】:

我正在尝试将输入值与多个表格单元格相乘,并将每个计算的结果分配给每个单元格右侧的表格单元格,第一行除外。 这是我的javascipt:

<script>
    function updateTable() {
        var x;
        var itemPrice = document.getElementById("inputPrice");
        var newPrice;
        for (i = 1; i < 50; i++) {
            x = document.getElementById("table1").rows[i].cells;
            newPrice = (parseInt(x[1].innerHTML)+1) * itemPrice;
            x[2].innerHTML = newPrice;
        }
    }
</script>

【问题讨论】:

  • itemPrice 将是一个 HTML 元素 - 你不能乘以一个对象 - 你需要将它转换为一个数字,就像你对单元格内容所做的那样
  • 您需要使用itemPrice.value。
  • 啊,完美。谢谢!

标签: javascript


【解决方案1】:

您忘记从 inputPrice 获取值。

 var itemPrice = document.getElementById("inputPrice").value;

你可以看一个例子here

【讨论】:

  • 啊,就是这样!谢谢。
猜你喜欢
  • 1970-01-01
  • 2022-08-07
  • 2017-11-26
  • 2018-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-06
相关资源
最近更新 更多