[英]javaScript multiply two numbers and show the result into third html input
I am trying to take a number from my html form and then multiply it with a number from my JavaScript function and displaying the result in another html input. 我试图从html表单中获取一个数字,然后将其与我的JavaScript函数中的一个数字相乘,然后在另一个html输入中显示结果。 Want I want is when the user click on the result input area then only the multiplication result shows. 我要的是,当用户单击结果输入区域时,仅显示乘法结果。
<script type="text/javascript">
function myFunction() {
var y = document.getElementById("km").value;
var z = 14;
var x = y * z;
document.getElementById("bill").innerHTML = x;
}
<table>
<tr>
<td>Total Kms Run</td>
<td><input type="text" id="km" name="km" required>
</tr>
<tr>
<td>Total Bill</td>
<td><input type="text" id = "bill" name="bill" required onclick="myFunction()">
</tr>
</table
You're adding the numbers, not multiplying them :). 您在添加数字,而不是将它们相乘:)。
That aside, input
s don't have innerHTML
, you need to set the value
: 除此之外, input
没有innerHTML
,您需要设置value
:
document.getElementById("bill").value = x;
Do Change like this 做这样的改变
<script type="text/javascript">
function myFunction() {
var y = document.getElementById("km").value;
var z = 14;
var x = y * z;
document.getElementById("bill").value = x;
}</script>
You have made many syntax mistakes in html 您在HTML中犯了许多语法错误
function myFunction() { var y = document.getElementById("km").value; var z = 14; var x = Number(y) * Number(z); document.getElementById("bill").value = x; }
<table> <tr> <td>Total Kms Run</td> <td><input type="text" id="km" name="km" required ></td> </tr> <tr> <td>Total Bill</td> <td><input type="text" id="bill" name="bill" required onclick="myFunction()"></td> </tr> </table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.