[英]Performing arithmetic operations on two inputs in JavaScript
I am trying to add two numbers that a user enters, and returning the sum, difference, product or the quotient of two values the user enters.我试图将用户输入的两个数字相加,并返回用户输入的两个值的总和、差、乘积或商。 For that, I made two inputs with a drop-down list between that.
为此,我做了两个输入,中间有一个下拉列表。 The drop down list has options to add, subtract, multiply and divide.
下拉列表有加、减、乘和除的选项。 What I am trying to do is perform the operation the user attempts to perform.
我想做的是执行用户尝试执行的操作。 You can see a demo here .
您可以在此处查看演示。
//Variables let firstNum = document.getElementById("num1"); let secondNum = document.getElementById("num2"); let result = document.getElementById("result"); //Event Listeners firstNum.addEventListener("input", mainFunction()); secondNum.addEventListener("input", mainFunction()); result.addEventListener("input", mainFunction()); //Main JavaScript function mainFunction() { if (document.getElementById("options").options[0]) { var one = parseFloat(firstNum.value) || 0; var two = parseFloat(secondNum.value) || 0; result.innerHTML = one+two; } }
* { font-family: helvetica; text-align: center; }
<h1>Calculator</h1> <form> <input type="number" id="num1" placeholder="First Number"/> <select id="options"> <option id="addition">+</option> <option id="subtraction">−</option> <option id="multiplication">✖</option> <option id="division">÷</option> </select> <input type="number" id="num2" placeholder="Second Number"/> <p id="result"></p> </form>
Please inform me if you find any errors.如果您发现任何错误,请通知我。 Thanks.
谢谢。
I have changed your code, the new version is here http://jsfiddle.net/v56fkaww/6/我已经更改了您的代码,新版本在这里http://jsfiddle.net/v56fkaww/6/
The error was that you should encapsulate the call of function into an anonymous function错误是你应该把函数的调用封装成一个匿名函数
//Event Listeners
firstNum.addEventListener("input",function(){mainFunction()});
secondNum.addEventListener("input",function(){mainFunction()});
Everything seems to be fine except for a small problem :除了一个小问题外,一切似乎都很好:
firstNum.addEventListener("input", mainFunction());
should be :应该 :
firstNum.addEventListener("input", mainFunction);
Since the function is already defined and this expects a function so you only need to pass a reference to the function.由于该函数已经定义并且这需要一个函数,因此您只需要传递对该函数的引用。
I added functionality for all four operators and shortened the code a bit我为所有四个运算符添加了功能并稍微缩短了代码
//Variables let firstNum = document.getElementById("num1"), secondNum = document.getElementById("num2"), result = document.getElementById("result"); //Event Listeners firstNum.addEventListener("input", mainFunction); secondNum.addEventListener("input", mainFunction); result.addEventListener("input", mainFunction); //Main JavaScript function mainFunction() { var one = +firstNum.value||0; // convert to number var two = +secondNum.value||0; // convert to number var opt = document.getElementById("options"); // this is the shorter than what you are using result.innerHTML = opt.options[0] ? one + two : opt.options[1] ? one - two : opt.options[2] ? one * two : one / two; }
* { font-family: helvetica; text-align: center; }
<h1>Calculator</h1> <form> <input type="number" id="num1" placeholder="First Number"/> <select id="options"> <option id="addition">+</option> <option id="subtraction">−</option> <option id="multiplication">✖</option> <option id="division">÷</option> </select> <input type="number" id="num2" placeholder="Second Number"/> <p id="result"></p> </form>
There is also a non standard way to evaluate operator without the need of performing a switch
or if
, this is using a Function constructor and execute inmediate.还有一种非标准的方法来评估运算符而无需执行
switch
或if
,这是使用函数构造函数并立即执行。 result.innerHTML = (new Function("return "+one+operator+two))();
//Variables
let firstNum = document.getElementById("num1");
let secondNum = document.getElementById("num2");
let result = document.getElementById("result");
let operator = document.getElementById("options").value;
//Event Listeners
//You should asign the function itself not the result like mainFunction()
firstNum.addEventListener("input", mainFunction);
secondNum.addEventListener("input", mainFunction);
result.addEventListener("input", mainFunction);
//All of this is like something.addEventListener("input", function(){//code});
function mainFunction() {
var one = parseFloat(firstNum.value) || 0;
var two = parseFloat(secondNum.value) || 0;
//evaluate the operator as a javascipt operator not just as string
result.innerHTML = (new Function("return "+one+operator+two))();
}
<td><input type="number" class="form-control text-center tot" id="input1"></td>
<td id="mark" class="text-center"></td>
<td><input type="number" class="form-control text-center tot" id="input2"></td>
<td id="total" class="text-center"></td>
function inputOper(operaterName) {
let input1 = Number($('#input1').val());
let input2 = Number($('#input2').val());
let result = $('#total');
if(operaterName == 'add'){
result = input1 + input2;
$('#mark').html('+');
}else if(operaterName == 'sub'){
result = input1 - input2;
$('#mark').html('-');
}else if(operaterName == 'mul'){
result = input1 * input2;
$('#mark').html('*');
}else if(operaterName == 'div'){
result = (input1 / input2).toFixed(2);
$('#mark').html('/');
}
$('#total').html(result)
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.