大侠,程序有没有哪里要改的,-*/都可以用,就是+不能,例如会出现1+6=16,敢问这是么子情况,
<!DOCTYPE html>
<html>
<head>
<title> 事件</title>
<script type="text/javascript">
function count(){
var x= document.getElementById("txt1").value;
//获取第一个输入框的值
var y= document.getElementById("txt2").value;
//获取第二个输入框的值
var select= document.getElementById("select").value
//获取选择框的值
var end;
x=parseInt(x);
y=parseInt(y);
switch(select){
case "+":
end=x+y;
break;
case "-":
end=x-y;
break;
case "*":
end=x*y;
break;
case "/":
end=x/y;
break;
}
//获取通过下拉框来选择的值来改变加减乘除的运算法则
document.getElementById("fruit").value=end;
//设置结果输入框的值
}
</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>可以这样x = parseInt(x)将字符串转换为int型整数
你这里的+是连接符,并不是运算符
也就是说你这里的X Y是字符串,被连接到一起了
转化为number可解决
不知道为什么+不好使,但是我改了一下间接的实现了加的功能,end=x-(-y)