用php做个计算器(加减乘除),两个文本框输入数字,第三个输出结果并_百度...
发布网友
发布时间:2022-04-29 15:35
我来回答
共1个回答
热心网友
时间:2022-04-18 14:14
不需要php呀
这样写的行不
<!DOCTYPE html>
<html>
<head>
<title>简单计算器</title>
</head>
<body>
<input type="text" name="first" id="first">
<select id="operate">
<option>+</option>
<option>-</option>
<option>*</option>
<option>/</option>
</select>
<input type="text" name="second" id="second">=
<input type="text" name="result" id="result">
<input type="button" name="运算" value="运算" onClick="operate()">
<script type="text/javascript">
function operate() {
var first = parseInt(document.getElementById("first").value);
var second = parseInt(document.getElementById("second").value);
var result = document.getElementById("result");
var opt = document.getElementById("operate");
if (0 == opt.selectedIndex) {
resultvalue = first + second;
}else if(1 == opt.selectedIndex){
resultvalue = first - second;
}else if (2 == opt.selectedIndex) {
resultvalue = first * second;
}else if (3 == opt.selectedIndex) {
if (second == 0) {
alert("除数不能为0");
}
resultvalue = first / second;
}
result.setAttribute("value",resultvalue);
}
</script>
</body>
</html>