gpt4 book ai didi

javascript - 将两个变量相乘并将总计插入输入 JavaScript on.click 命令

转载 作者:行者123 更新时间:2023-12-03 06:24:03 25 4
gpt4 key购买 nike

我有两个输入宽度和高度来计算三 Angular 形的面积。当您在每个输入中输入宽度和高度的数字时,我需要一个提交按钮来乘以宽度 x 高度并将总面积插入输入中。

HTML:

<p id="instructions" class="Main">Enter the width and height of your triangle to calculate the area.</p>

<!--Main div-->
<form id="mainform">
Width (b):
<input type="Text" id="width" placeholder="width" >
<div>
Height(h):
<input type="text" id="height" placeholder="height">
</div>
<button type="button" id="total" onclick="calculateArea()">Calculate Area</button>
</form>

<!--Divider-->
<form>
<div id="divider">
</div>
<div id="area">
The area is:
<input type="text" name="txtarea" id="total">
</div>
</form>
</div>

JavaScript:

    function calculateArea() {
var width = document.getElementById('width').value;
var height = document.getElementById('height').value;
var total = document.getElementById('total');
total.value = width * height;
}

最佳答案

  • 两者都有相同的 ID“total”。我将其中一个命名为“产品”。
  • total.value = myResult; 还有一个拼写错误。

 function calculateArea() {
var width = document.getElementById('width').value;
var height = document.getElementById('height').value;
var total = document.getElementById('product');
var myResult = width * height;
total.value = myResult;
}
<p id="instructions" class="Main">Enter the width and height of your triangle to calculate the area.</p>

<!--Main div-->
<form id="mainform">
Width (b):
<input type="Text" id="width" placeholder="width">
<div>
Height(h):
<input type="text" id="height" placeholder="height">
</div>
<button type="button" id="total" onclick="calculateArea()">Calculate Area</button>
</form>

<!--Divider-->
<form>
<div id="divider">
</div>
<div id="area">
The area is:
<input type="text" name="txtarea" id="product">
</div>
</form>
</div>

关于javascript - 将两个变量相乘并将总计插入输入 JavaScript on.click 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38729692/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com