[英]Multiply two cells in my HTML table and automatically put the result in the third with javascript. Withot Jquery
[英]javaScript multiply two numbers and show the result into third html input
我试图从html表单中获取一个数字,然后将其与我的JavaScript函数中的一个数字相乘,然后在另一个html输入中显示结果。 我要的是,当用户单击结果输入区域时,仅显示乘法结果。
<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
您在添加数字,而不是将它们相乘:)。
除此之外, input
没有innerHTML
,您需要设置value
:
document.getElementById("bill").value = x;
做这样的改变
<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>
您在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.