加法變成字符串相加了怎么辦?
function add(x,y){
? ? ? return x+y;
? }
? function jian(x,y){
? ? ? return x-y;
? }
? function chen(x,y){
? ? ? return x*y;
? }
? function chu(x,y){
? ? ? return x/y;
? }
? ?function count(){
? ? ?var res;??
? ? //獲取第一個輸入框的值
? ? var a=document.getElementById("txt1").value;
//獲取第二個輸入框的值
var b=document.getElementById("txt2").value;
//獲取選擇框的值
var c=document.getElementById("select").value;
//獲取通過下拉框來選擇的值來改變加減乘除的運(yùn)算法
if(c=="+"){
? ? res=add(a,b);
}else if(c=="-"){
? ? res=jian(a,b);
}else if(c=="*"){
? ? res=chen(a,b);
}else{
? ? res=chu(a,b);
}
? ? //設(shè)置結(jié)果輸入框的值?
? ? document.getElementById("fruit").value=res;
? ?}
2018-08-08
var a = parseInt(document.getElementById("txt1").value);//獲取第一個輸入框的值
var? b = parseInt(document.getElementById("txt2").value);//獲取第二個輸入框的值
var? oper = document.getElementById("select").value;//獲取選擇框的值
2018-08-11
既然"+"會被識別為字符串疊加符號,可以試著用z=x-(-y)來實(shí)現(xiàn)z=x+y的功能。