<!DOCTYPE html>
<html>
<head>
<title> 事件</title>
<script type="text/javascript">
function count(){
var output;
var input1=parseInt(document.getElementById("txt1").value) ;
var input2=parseInt(document.getElementById("txt2").value) ;
var selec=document.getElementById("select").value ;
switch(selec)
case "+":output=input1+input2;break;
case "-":output=input1-input2;break;
case "*":output=input1*input2;break;
default:output=input1/input2;break;
document.getElementById("fruit").value=output;
}
</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>
function count(){
var output;
var input1=parseInt(document.getElementById("txt1").value) ;
var input2=parseInt(document.getElementById("txt2").value) ;
var selec=document.getElementById("select").value ;
switch(selec){
case "+":output=input1+input2;break;
case "-":output=input1-input2;break;
case "*":output=input1*input2;break;
case "/":output=input1/input2;break;
default: output="default";break;
}
document.getElementById("fruit").value=output;
}
谢谢你!
switch(selec){ }
你少了一组switch后的大括号
switch(selec)
case "+":output=input1+input2;break;
case "-":output=input1-input2;break;
case "*":output=input1*input2;break;
default:output=input1/input2;break;
你这一段少了{ }