我必须将算术运算的返回值显示给另一个函数。
function calCaulation(e) {
var x = parseFloat(document.getElementById("first").value);
var y = parseFloat(document.getElementById("second").value);
var z = document.getElementById("oper").value;
switch(z){
case("+"):
var a = "Sum:" + ((x + y).toFixed(2));
break;
case("-"):
var a = "Sub:" + ((x - y).toFixed(2));
break;
case("*"):
var a = "Mul:" + ((x * y).toFixed(2));
break;
case("/"):
var a = "Div:" + ((x / y).toFixed(2));
break;
default:
var a = "Invalid Operator";
}
return a;
}
function dispResult (a) {
document.getElemntById("demo").innerHTML = a;
}
当我尝试这样做时,结果只显示一次,我想要为输入值的每次更改调用函数。
答案 0 :(得分:0)
很难从你的问题中准确掌握你想要的东西,但这是一个我把它放在一起调用不同算术函数的例子:
https://jsfiddle.net/n0pchkgr/
document.getElementById("confirm").onclick = function(){
var first = document.getElementById('first').value;
var second = document.getElementById('second').value;
document.getElementById('output').innerHTML = add(first,second);
document.getElementById('multipliedOutput').innerHTML = multiplyTwoNumbers(first,second);
};
function add(){
var returnAmount = 0;
for (var i = 0; i < arguments.length; i++){
returnAmount+= Number(arguments[i]) || 0;
}
return returnAmount;
}
function multiplyTwoNumbers(a,b){
return a*b;
}