我的 js 代码有什么问题。结果总是未定义?

我刚开始学习 javascript,我试图像计算器一样创建只是为了添加、减去、乘以和除以两个数字。但结果总是不确定的。你能告诉我问题出在哪里吗?


    //Two functions

    function output() {


     var input1 = document.getElementById("nr1").value;

     var input2 = document.getElementById("nr2").value;

     var result = document.getElementById("result");


      result.innerHTML = calculate(input1, input2).value;

     }


     function calculate(input1, input2) {


       var add = document.getElementById("add");

       var sub = document.getElementById("sub");

       var mul = document.getElementById("mul");

       var div = document.getElementById("div");

       var rez

       //How to know when we click one button?


       if (add == true) {

          rez = parseFloat(input1 + input2);

          return rez;

       } else if (sub == true) {

          rez = parseFloat(input1 - input2);

          return rez;

       } else if (mul == true) {

          rez = parseFloat(input1 * input2);

          return rez;

       } else if (div == true) {

          rez = parseFloat(input1 / input2);

          return rez;

       } else {

          return 0;

   }

}


浮云间
浏览 155回答 2
2回答

Helenr

calculate是一个返回数字的函数。数字没有value属性。你想要的只是函数内部的返回值,所以:改变:result.innerHTML = calculate(input1, input2).value;至:result.innerHTML = calculate(input1, input2);而且,正如其他人在评论中指出的那样,您的if声明试图将页面中的元素与true.相反,您应该检查元素包含的内容。如果是表单域(输入、选择、文本区域等),请检查元素的.value属性。如果没有,请检查其.textContent属性。现在,根据您的用例,您需要知道单击了哪个按钮,以便您可以执行正确的数学运算。为此,您需要处理click事件并执行正确的操作。请参阅以下内容以及可能的解决方案的评论:// Get references to the elements you'll work withlet input1 = document.getElementById("one");let input2 = document.getElementById("two");let result = document.getElementById("result");// Set up a click event hanlder at the document leveldocument.addEventListener("click", calculate);function calculate(event) {&nbsp; // First, check to see if the event originated at one of the math buttons&nbsp; if(event.target.classList.contains("operator")){&nbsp; &nbsp; // One of the operator buttons was clicked.&nbsp; &nbsp; // Get the textContent of that button and do the appropriate math&nbsp; &nbsp; let operation = event.target.textContent;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; if(operation === "+"){&nbsp; &nbsp; &nbsp; // The prepended + sign converts the string value to a number&nbsp; &nbsp; &nbsp; result.textContent = +input1.value + +input2.value;&nbsp; &nbsp; } else if(operation === "-") {&nbsp; &nbsp; &nbsp; result.textContent =&nbsp; +input1.value - +input2.value;&nbsp; &nbsp; } else if(operation === "*") {&nbsp; &nbsp; &nbsp; result.textContent =&nbsp; +input1.value * +input2.value;&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; result.textContent =&nbsp; +input1.value / +input2.value;&nbsp; &nbsp; }&nbsp; }}<input id="one"><br><input id="two"><button type="button" class="operator">+</button><button type="button" class="operator">-</button><button type="button" class="operator">*</button><button type="button" class="operator">/</button><br><span id="result"></span>

慕慕森

你为什么在 calculate(input1, input2).value 中做 .value ?因为你不是返回一个对象,而是一个数值,所以你不需要做 .value使用 onclick 功能添加点击事件
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript