3 回答
TA贡献20条经验 获得超4个赞
<select name="xiala" size="1" >
<option value="+" selected="selected" id="yusuanfu">+</option>
<option id="yusuanfu" value="-">-</option>
<option id="yusuanfu" value="*">*</option>
<option id="yusuanfu" value="/">/</option>
</select>
这一部分代码有问题,
<select name="xiala" id="yunsuanfu" size="1" >
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
我的全部代码,用你的经过删改的,是通过点击完成你的作用的
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" >
function yunsuan(){
var _num1=document.getElementById("_num1").value;
_num1 =parseInt(_num1);
var _num2=document.getElementById("_num2").value;
_num2 =parseInt(_num2);
var yunsuanfu=document.getElementById("yunsuanfu").value;
var sum;
switch(yunsuanfu){
case '+':
sum=_num1+_num2;
break;
case '-' :
sum=_num1-_num2;
break;
case '*' :
sum=_num1*_num2;
break;
case '/' :
sum=_num1/_num2;
break;
default:
break;
}
document.getElementById("sum").innerHTML = sum;
}
</script>
</head>
<body>
<input type="text" value="5" id="_num1"/>
<select name="xiala" id="yunsuanfu" size="1" >
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type="text" value="6" id="_num2"/>
<input type="button" value="=" onclick="yunsuan()">
<span id="sum"> </span>
</body>
</html>
TA贡献1条经验 获得超0个赞
<title></title>
<script type="text/javascript" >
window.onload=function(){
var _num1=document.getElementById("_num1").value;
_num1 =parseInt(_num1);
var _num2=document.getElementById("_num2").value;
_num2 =parseInt(_num2);
var yunsuanfu=document.getElementById("yunsuanfu").value;
var sum;
switch(yunsuanfu){
case '+':
sum=_num1+_num2;
break;
case '-' :
sum=_num1-_num2;
break;
case '*' :
sum=_num1*_num2;
break;
case '/' :
sum=_num1/_num2;
break;
default:没有此语句 ;
break;
}
document.getElementById("sum").innerHTML=sum;
</script>
</head>
<body>
<input type="text" value="5" id="_num1"/>
<select name="xiala" size="1" >
<option value="+" selected="selected" id="yusuanfu">+</option>
<option id="yusuanfu" value="-">-</option>
<option id="yusuanfu" value="*">*</option>
<option id="yusuanfu" value="/">/</option>
</select>
<input type="text" value="6" id="_num2"/>
=
<span id="sum"> </span>
</body>
</html>
大神帮看看
TA贡献32条经验 获得超24个赞
其实你可以获取到select的值 ,然后套用 switch 进行比较 ,比如你选择 '+'的操作
switch(tag){ case '+': sum = num1+num2; break; case '-': ............. }
可以这样来操作, 你判断选择符那里有问题 , 因为 你用的是 sum = num1+num2 , 所以执行的永远是第一条语句 不懂可以继续提问
添加回答
举报