<!DOCTYPE html>
<html>
<head>
<title> 事件</title>
<script type="text/javascript">
function count(){
var t1=parselent(document.getElementById("txt1").value);
var t2=parselent(document.getElementById("txt2").value);
//获取第一个输入框的值
//获取第二个输入框的值
//获取选择框的值
var se=document.getElementById("select").value;
//获取通过下拉框来选择的值来改变加减乘除的运算法则
//设置结果输入框的值
var req;
switch(se){
case "+": req=t1+t2;break;
case "-":req=t1-t2;break;
case "*":req=t1*t2;break;
case "/":req=t1/t2;break;
}
req=document.getElementById("fruit").value;
}
</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>
switch(se){
case "+": req=t1+t2;break;
case "-":req=t1-t2;break;
case "*":req=t1*t2;break;
case "/":req=t1/t2;break;
}
里面的case "+":req=t1+t2要改为case "+":req=parseInt(t1)+parseInt(t2);后面三行也得这么改;
因为t1,t2在这里都是字符串格式,得用parseInt()函数将字符串改为整数模式才能进行加减等运算;
记住parseInt中的I是大写i;不是小写l,我自己就弄错了;
function count(){
var txt1 = document.getElementById('txt1').value;
var txt2 = document.getElementById('txt2').value;
var select = document.getElementById("select");
var index = select.selectedIndex;
var select_val = select.options[index].value;
var fruit = document.getElementById('fruit');
var num = "";
switch(select_val){
case "+":
num = parseFloat(txt1)+ parseFloat(txt2);
break;
case "-":
num = txt1-txt2;
break;
case "*":
num = txt1*txt2;
break;
case "/":
num = txt1/txt2;
break;
}
fruit.value = num;
}