将两个变量相乘,并将total插入到输入的JavaScript中.点击命令
Multiplying two variables and inserting total into input JavaScript on.click command
我有两个输入宽度和高度来计算三角形的面积。当你在每个输入中键入宽度和高度的数字时,我需要一个提交按钮来乘以宽度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>
您有一个id为"total"的错误元素。按钮不应该,输入应该。
您应该按Id选择元素,然后将结果作为值分配给元素
因此,在您的情况下,应该是:从按钮中删除总id。现在您只有一个具有唯一id"total"的元素document.getElementById("txtArea").value=结果;
id="total"
有两个元素。将按钮上的id属性更改为其他属性或将其删除- 您在javascript函数的最后一行将
myResult
大写。javascript中的大小写很重要
您可以编写return myResult
,然后calculateArea函数将返回myResult。
此外,"id"用于唯一的元素-对多个元素使用"class"。
相关文章:
- 我如何在数字插入中使用逗号,这样它就不会'不要破坏我的输入字段
- 如何jQuery-Add按钮最多插入10个新输入
- 为更改的输入返回插入符号位置的逻辑
- 通过javascript以编程方式将文件插入HTML输入
- 如何在dom元素中插入输入标记数据插件
- 如何使用JS在中间输入文本中动态插入字符
- 将文件插入到另一个输入的输入
- 我想插入要链接的输入值
- 在列表项中插入错误值的文本输入
- 从输入中提取数字,并插入一个等式javascript
- 在DOM中以img的形式插入文件输入
- 在输入文本框的文本中间插入键盘字符值
- 在输入中每 4 个字符后插入短划线
- 将JS中的一个变量插入Django表单输入字段
- 将数据插入输入值-AngularJS
- 在用户为下一次输入插入数据后清空文本字段
- 如何在编码器中使用具有相同名称的多个输入插入数据
- 将用户输入插入到对象数组中
- 将多个具有相同名称的文本输入插入到 MySql 中的一个单元格中
- 使用Switch语句来更改值&根据预先存在的值为文本输入插入标签