日期:2014-05-17  浏览次数:20730 次

简单计算器(html+js)
<html>

<head>
<title>简单计算器</title>
<h1 align='center'>简单计算器</h1>
<script language="javascript">
function counter() {
var firstNum = parseInt(document.getElementById("firstNum").value);
var lastNum = parseInt(document.getElementById("lastNum").value);
var result = 0;
var rule = document.getElementById("rule").value;
switch(rule) {
case "+" :
result = firstNum + lastNum;
break;
case "-" :
result = firstNum - lastNum;
break;
case "*" :
result = firstNum * lastNum;
break;
case "/" :
result = firstNum / lastNum;
break;
}
document.getElementById("result").value = result;
}

</script>
</head>

<body>
<p align='center'>
<input type="text" name="firstNum" id="firstNum">

<select name="rule" id="rule">
<option value="+" id="+">+</option>
<option value="-" id="-">-</option>
<option value="*" id="*">*</option>
<option value="/" id="/">/</option>
</select>

<input type="text" name="lastNum" id="lastNum">

<input type="button" value="=" onclick="counter()">

<input type="text" name="result" id="result">
</p>
<p align="left">
<img src="logoo.jpg">
<img src="logo.jpg">
</p>
</body>

</html>


------解决方案--------------------
呵呵,你的除数为0的情况不作处理吗?
------解决方案--------------------
没写form也能行?
你的document.getElementById("result").value = result;不报错?