简体   繁体   English

如何为简单计算器传递用户输入

[英]How to pass user input for simple calculator

I am self-learning JavaScript and I am a bit confused on how to pass the user input as parameters.我正在自学 JavaScript,我对如何将用户输入作为参数传递有点困惑。 I still have the mindset of Python, which is why I think I am struggling.我仍然有Python的心态,这就是为什么我认为我在挣扎。 Any help is appreciated.任何帮助表示赞赏。

 function userInput(num1, num2) { var num1 = parseInt(document.getElementById("number1").value); var num2 = parseInt(document.getElementById("number2").value); return num1, num2; } function addition(num1, num2) { document.getElementById("result").innerHTML = (num1 + num2); alert("Your answer is " + (num1 + num2)); return addition; }
 <form name="calculator"> Number 1: <input type="text" id="number1"> Number 2: <input type="text" id="number2"> <input type="button" value="ADD" onclick="javaScript:addition()"> <input type="button" value="SUB" onclick="javaScript:subtraction()"> <input type="button" value="MUL" onclick="javaScript:multiply()"> <input type="button" value="DIV" onclick="javaScript:division()"> <input type="button" value="MOD" onclick="javaScript:modulus()"> </form> <p>The result is: <br> <span id="result"></span> </p>

You need to get the values in addition and you don't need userInput function. 你需要得到的值, addition ,你不需要userInput功能。 Below is the updated code for you: 以下是为您更新的代码:

 function addition() { var num1 = parseInt(document.getElementById("number1").value); var num2 = parseInt(document.getElementById("number2").value); var sumOfNumbers = num1 + num2; document.getElementById("result").innerHTML = sumOfNumbers; alert("Your answer is " + sumOfNumbers); } 
 <form name="calculator"> Number 1: <input type="text" id="number1"> Number 2: <input type="text" id="number2"> <input type="button" value="ADD" onclick="javaScript:addition()"> <input type="button" value="SUB" onclick="javaScript:subtraction()"> <input type="button" value="MUL" onclick="javaScript:multiply()"> <input type="button" value="DIV" onclick="javaScript:division()"> <input type="button" value="MOD" onclick="javaScript:modulus()"> </form> <p>The result is: <br> <span id="result"></span> </p> 

Create a button and call the add function 创建一个按钮并调用添加功能

 function add(num1, num2) { var num1 = parseInt(document.getElementById("number1").value); var num2 = parseInt(document.getElementById("number2").value); addition(num1, num2) return num1, num2; } function addition(num1, num2) { document.getElementById("result").innerHTML = (num1 + num2); alert("Your answer is " + (num1 + num2)); return addition; } 
 <input type="text" id="number1"> <input type="text" id="number2"> <button onclick="add()">Add</button> <div id="result"></div> 

You do not need to pass parameters. 您不需要传递参数。 Try the following: 请尝试以下操作:

 function addition(){ var num1 = parseInt(document.getElementById("number1").value); var num2 = parseInt(document.getElementById("number2").value); document.getElementById("result").textContent = num1 + num2; alert("Your answer is " + (num1 + num2)); } 
 Number1: <input type="text" id="number1"/><br> Number2: <input type="text" id="number2"/><br> <button id="add" onclick="addition()">Add</button> Result: <label type="text" id="result"/> 

Different way of doing this using event listener. 使用事件侦听器执行此操作的不同方法。 Which is a bit cleaner looking in my opinion. 我认为这看起来更干净。 Just giving you another option. 只是给您另一个选择。

JSFIDDLE: https://jsfiddle.net/py70618w/10/ <--- you can see it live in action. JSFIDDLE: https ://jsfiddle.net/py70618w/10/ <---您可以看到它实时运行。

HTML HTML

<form name="calculator">
 Number 1: <input type="text" id="number1"> Number 2: <input type="text" 
 id="number2">

 <input type="button" id="add" value="ADD">
 <input type="button" value="SUB" onclick="javaScript:subtraction()">
 <input type="button" value="MUL" onclick="javaScript:multiply()">
 <input type="button" value="DIV" onclick="javaScript:division()">
 <input type="button" value="MOD" onclick="javaScript:modulus()">

</form>

<p>The result is: <br>
 <span id="result"></span>
</p>

Refactored Javascript : 重构的Javascript

document.getElementById("add").addEventListener("click", function(){
var num1 = parseInt(document.getElementById("number1").value);
var num2 = parseInt(document.getElementById("number2").value);
document.getElementById("result").innerHTML = (num1+num2);
});

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

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