如何制作一个计算器,但有两个以上的输入?

时间:2012-06-20 23:09:20

标签: javascript calculator

这里是一个链接到我的代码的基础....但我希望有超过2个区域来键入数字,如果这是有道理的...所以数字1,数字2,然后数字3 ...等

LINK

我试图将所有内容复制下来,然后添加

c=Number(document.calculator.number3.value); 
d=Number(document.calculator.number4.value);

<!-- Here user will enter 3rd number. -->   
Number 3: <input type="text" name="number3">  
<!-- Here user will enter 4th number. -->   
Number 4: <input type="text" name="number4">  

并将结果更改为e = a + b + c + d 但随后计算器停止运作

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:3)

尝试 - http://jsfiddle.net/GmDuE/

HTML

Number 1: <input type="text" name="number1">
Number 2: <input type="text" name="number2">
Number 3: <input type="text" name="number3">
Number 4: <input type="text" name="number4">

JS

<script language="javascript" type="text/javascript">
function multiply(){
a=Number(document.calculator.number1.value);
b=Number(document.calculator.number2.value);
c=Number(document.calculator.number3.value);
d=Number(document.calculator.number4.value);
m=a*b*c*d;
document.calculator.total.value=m;
}

function addition(){
a=Number(document.calculator.number1.value);
b=Number(document.calculator.number2.value);
c=Number(document.calculator.number3.value);
d=Number(document.calculator.number4.value);
m=a+b+c+d;
document.calculator.total.value=m;
}
</script>

etc.