请问6-11编程为什么直接“var result=document.getElementById("fruit").value;”,再在switch中赋值计算结果,点击等号无效,也不报错呢?具体见代码,谢谢!
<!DOCTYPE html>
<html>
<head>
<title> 事件</title>
<script type="text/javascript">
function count(){
var num1=parseInt(document.getElementById("txt1").value);
var num2=parseInt(document.getElementById("txt2").value);
var select=document.getElementById("select").value;
//为什么对result直接取值后,在switch中再赋值不起作用呢?
var result=document.getElementById("fruit").value;
switch(select){
case "+":
result=num1+num2;
break;
case "-":
result=num1-num2;
break;
case "*":
result=num1*num2;
break;
case "/":
result=num1/num2;
break;
}
}
</script>
</head>
<body>
<input type='text' id='txt1' />
<select id='select'>
<option value='+'>+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type='text' id='txt2' />
<input type='button' value=' = ' onclick="count()"/> <!--通过 = 按钮来调用创建的函数,得到结果-->
<input type='text' id='fruit' />
</body>
</html>