我正在尝试用输入创建一个计算器,但它的工作原理错误,为什么?

时间:2016-08-15 15:51:20

标签: javascript html switch-statement calculator reset

我正在尝试使用HTML输入创建一个计算器,但它无法正常工作,当我按下“计算”时,它会显示正确答案并立即刷新页面,但我不想刷新 这是我的代码,为什么它不工作? document.getElementById("calc").onclick = function() {myFunction()};

function myFunction() {
 if (isNaN(document.getElementById("num1").value) || isNaN(document.getElementById("num2").value)) {
   document.getElementById("value").innerHTML = "Please enter number!";

 }
else {
  switch (document.getElementById("opr").value) {
    case "+": 
      document.getElementById("value").innerHTML = parseInt(document.getElementById("num1").value) +      parseInt(document.getElementById("num2").value) ;
      break;
    case "-": document.getElementById("value").innerHTML = parseInt(document.getElementById("num1").value) -   parseInt(document.getElementById("num2").value) ;
      break;
    case "*": document.getElementById("value").innerHTML = parseInt(document.getElementById("num1").value) * parseInt(document.getElementById("num2").value) ;
      break;
    case "/": document.getElementById("value").innerHTML = parseInt(document.getElementById("num1").value) / parseInt(document.getElementById("num2").value) ;
      break;
    default: document.getElementById("value").innerHTML = "Something went wrong, please try again";
      break;
  } 
  }  
  }

0 个答案:

没有答案