js中select的点击触发
我想要点击中间的加减乘除,然后自动计算结果,该怎么弄?<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""h...
我想要点击中间的加减乘除,然后自动计算结果,该怎么弄?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>计算器</title>
</head>
<script>
function add(){
a=parseInt(document.getElementById('shu1').value)
b=parseInt(document.getElementById('shu2').value)
d=document.getElementById('suanfa').value
if (d=="1"){
c=a+b
}
else if(d=="2"){
c=a-b
}
else if (d=="3"){
c=a*b}
else
{c=a/b}
document.getElementById('jieguo').value=c
}
</script>
<body>
<div style="text-align:center">
超级简单计算器
<form>
<input type="text" id="shu1" onkeyup="add()"/>
<select id="suanfa">
<option value="1">+</option>
<option value="2">-</option>
<option value="3">*</option>
<option value="4">/</option>
</select>
<input type="text" id="shu2" onkeyup="add()"/>
<input type="button" value="=" onmousemove="add()">
<input type="text" id="jieguo" />
</form></div>
</body>
</html> 展开
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>计算器</title>
</head>
<script>
function add(){
a=parseInt(document.getElementById('shu1').value)
b=parseInt(document.getElementById('shu2').value)
d=document.getElementById('suanfa').value
if (d=="1"){
c=a+b
}
else if(d=="2"){
c=a-b
}
else if (d=="3"){
c=a*b}
else
{c=a/b}
document.getElementById('jieguo').value=c
}
</script>
<body>
<div style="text-align:center">
超级简单计算器
<form>
<input type="text" id="shu1" onkeyup="add()"/>
<select id="suanfa">
<option value="1">+</option>
<option value="2">-</option>
<option value="3">*</option>
<option value="4">/</option>
</select>
<input type="text" id="shu2" onkeyup="add()"/>
<input type="button" value="=" onmousemove="add()">
<input type="text" id="jieguo" />
</form></div>
</body>
</html> 展开
2个回答
展开全部
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>计算器</title>
</head>
<script>
function calc() {
var reg = /^\s*|\s$/g;
var first = document.getElementById("first").value.replace(reg, "");
var opera = document.getElementById("opera").value;
var second = document.getElementById("second").value.replace(reg, "");
var result = document.getElementById("result");
try {
var temp = eval(first + opera + second);
if (temp - parseFloat(temp) >= 0) {
result.value = temp;
} else {
result.value = "";
}
} catch (e) {
}
return false;
}
</script>
<body>
<div style="text-align:center">
超级简单计算器
<form>
<input type="text" id="first" onkeyup="calc()"/>
<select id="opera" onchange="calc();">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type="text" id="second" onkeyup="calc()"/>
<input type="button" value="=" onclick="calc()">
<input type="text" id="result" />
</form></div>
</body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询