简体   繁体   English

如何执行计算并将其显示在JavaScript的输入框中?

[英]How to perform a calculation and display it in an input box in JavaScript?

I am trying to do a simple calculation of two numbers that lets the user either multiply them or divide them. 我正在尝试对两个数字进行简单的计算,以使用户可以将它们相乘或相除。 I know there are other ways do do this with way less code but I'd like to figure out why my result box is displaying NaN. 我知道还有其他方法可以用更少的代码来执行此操作,但我想弄清楚为什么我的结果框显示NaN。 I've tested to make sure the userInput1 and userInput2 variables are both indeed numbers but it looks like something is happening inside the other functions to make it NaN.` 我已经进行测试,以确保userInput1和userInput2变量的确是数字,但是看起来其他函数内部发生了某些事情,使其变为NaN。

 /*eslint-env browser*/ var $ = function(id) { "use strict"; return window.document.getElementById(id); }; var userInput1 = parseInt($('input1'), 10); var userInput2 = parseInt($('input2'), 10); //TO TEST console.log(typeof userInput1); console.log(typeof userInput2); function doTheMultiCalc() { "use strict"; var multiCalculation = userInput1 * userInput2; //TO TEST console.log(typeof(multiCalculation)); $('resultOutput').value = multiCalculation; return multiCalculation; } function doTheDivCalc() { "use strict"; var divCalculation = userInput1 / userInput2; //TO TEST console.log(typeof(divCalculation)); $('resultOutput').value = divCalculation; return divCalculation; } //Event Handler Function function init() { "use strict"; $('multiply').addEventListener('click', doTheMultiCalc); $('divide').addEventListener('click', doTheDivCalc); } window.addEventListener('load', init); 
 <label for="input1">1st Number</label><input id="input1"><br> <label for="input2">2nd Number</label><input id="input2"><br> <button id="multiply" type="button">Multiply</button> <button id="divide" type="button">divide</button><br> <label for="resultOutput">Result is</label> <input id="resultOutput"> 

` `

I commend your trying to figure this out. 我赞扬您试图弄清楚这一点。 So there are two issues I see. 因此,我看到了两个问题。

First and foremost, you're not actually getting the value of the inputs. 首先,您实际上并没有获得输入的价值。 You need to use $('input1').value to retrieve the actual value of the input. 您需要使用$('input1').value检索输入的实际值。

You also need to fetch that value before doing each operation. 您还需要在执行每个操作之前获取该值。 Here you see it in action: 在这里,您可以看到它的实际效果:

 /*eslint-env browser*/ var $ = function(id) { "use strict"; return window.document.getElementById(id); }; function doTheMultiCalc() { "use strict"; var userInput1 = parseInt($('input1').value, 10); var userInput2 = parseInt($('input2').value, 10); var multiCalculation = userInput1 * userInput2; $('resultOutput').value = multiCalculation; return multiCalculation; } function doTheDivCalc() { "use strict"; var userInput1 = parseInt($('input1').value, 10); var userInput2 = parseInt($('input2').value, 10); var divCalculation = userInput1 / userInput2; $('resultOutput').value = divCalculation; return divCalculation; } //Event Handler Function function init() { "use strict"; $('multiply').addEventListener('click', doTheMultiCalc); $('divide').addEventListener('click', doTheDivCalc); } window.addEventListener('load', init); 
 <label>1st Number</label><input id="input1"><br><br> <label>2nd Number</label><input id="input2"><br><br> <button id="multiply">Multiply</button> <button id="divide">divide</button><br><br> <label>Result is</label> <input id="resultOutput"> 

This might work. 这可能有效。

 var $ = function (id) { "use strict"; return document.getElementById(id); }; function doTheMultiCalc() { "use strict"; var multiCalculation = parseInt($('input1').value, 10) * parseInt($('input2').value, 10); //TO TEST $('resultOutput').value = multiCalculation; } function doTheDivCalc() { "use strict"; var divCalculation = parseInt($('input1').value, 10) / parseInt($('input2').value, 10); //TO TEST $('resultOutput').value = divCalculation; } //Event Handler Function function init() { "use strict"; $('multiply').addEventListener('click', doTheMultiCalc); $('divide').addEventListener('click', doTheDivCalc); } window.addEventListener('load', init); 
  <label>1st Number</label><input id = "input1"><br><br> <label>2nd Number</label><input id = "input2"><br><br> <button id = "multiply">Multiply</button> <button id = "divide">divide</button><br><br> <label>Result is</label> <input id = "resultOutput"> 

I found the solution for your app.js yesterday but i lost my internet and i was unable to respond 昨天我找到了适用于您app.js的解决方案,但我失去了互联网,无法响应

Annother solution create a function getInput() so there you can test if the value is an number ... is not empty etc... : 另一个解决方案创建一个函数getInput(),以便您可以在其中测试该值是否为数字...不为空等...:

 var $ = function (id) { "use strict"; return window.document.getElementById(id); }; function getInput(){ userInput1 = parseInt($('input1').value, 10); userInput2 = parseInt($('input2').value, 10); } //TO TEST function doTheMultiCalc() { "use strict"; getInput(); var multiCalculation = userInput1 * userInput2; //TO TEST console.log(typeof (multiCalculation)); $('resultOutput').value = multiCalculation; return multiCalculation; } function doTheDivCalc() { "use strict"; getInput(); var divCalculation = userInput1 / userInput2; //TO TEST console.log(typeof (divCalculation)); $('resultOutput').value = divCalculation; return divCalculation; } //Event Handler Function function init() { "use strict"; $('multiply').addEventListener('click', doTheMultiCalc); $('divide').addEventListener('click', doTheDivCalc); } document.addEventListener('DOMContentLoaded', init); 
 <!DOCTYPE html> <html lang="fr"> <head> <meta charset="UTF-8"> <title>oLogin</title> <link rel="stylesheet" href="../css/reset.css"> <!-- Font --> <link rel="stylesheet" href="../css/style.css"> </head> <body> <label>1st Number</label><input id ="input1"><br><br> <label>2nd Number</label><input id ="input2"><br><br> <button id ="multiply">Multiply</button> <button id ="divide">divide</button><br><br> <label>Result is</label> <input id ="resultOutput"> </div> <script src="../js/app.js"></script> </body> </html> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM