简体   繁体   English

javaScript将两个数字相乘并将结果显示到第三个html输入中

[英]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.

相关问题 将我的 HTML 表中的两个单元格相乘,并自动将结果与 javascript 放在第三个单元格中。 无Jquery - Multiply two cells in my HTML table and automatically put the result in the third with javascript. Withot Jquery 将来自两个不同输入的值相乘并显示在第三个输入中 - Multiply values from two different input and show in the third input 将两项相乘并在第三项中自行生成结果 - Multiply Two items and result self generate in Third 减去两个文本字段并在 javascript 或 jquery 中的第三个字段上显示结果 - Subtract two text fields and show the result on a third in javascript or jquery 使用Javascript比较HTML中的两个输入数字? - Using Javascript to compare two input numbers in HTML? 如何将两列相乘并显示结果 - How to multiply two columns and show the result 将 django 表格中的两个字段相乘,并用结果填充第三个字段 - Multiply two fields in django form and populate third filed with the result 在循环内将两个文本框相乘,并在第三个文本框上显示结果 - Multiply two textbox inside loop and display result on third textbox 使用javascript计算两个输入框中的值并在第三个输入框中获得结果? - Calculate values in two input boxes and get result in third input box using javascript? Javascript - 将两个输入字段相乘并显示 - Javascript - Multiply two input fields together and display
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM