如何使用此代码段显示其他值?

时间:2019-09-09 00:18:02

标签: javascript

我正在尝试构建一个计算器,用户可以在其中按下数字值,而不是每次按下时都会在输入(type =“ text”)元素上显示确切的值。我的JS代码有效,但是无论按下什么数字按钮,唯一显示的数字都是1。附带说明一下,我正在使用一个事件处理程序,该事件处理程序传递我在最顶部声明​​的全局变量(即一,二...)的参数来激活JS代码。

在第一个切换条件下,我尝试增加(num),然后将其设置为等于(#displayField),但这只是阻止了代码运行。我什至尝试使用(if ... else)条件语句,但结果相同。

//Calculator buttons

let one = document.querySelector("#one").value;
let two = document.querySelector("#two").value;
let three = document.querySelector("#three").value;
let four = document.querySelector("#four").value;
let five = document.querySelector("#five").value;
let six = document.querySelector("#six").value;
let seven = document.querySelector("#seven").value;
let eight = document.querySelector("#eight").value;
let nine = document.querySelector("#nine").value;

//Input Element
//#displayField is the input(type="text") field.
/*pass the arguments (variables one, two...) from the HTML document event handlers.*/

function inputs(num) {
  switch(num) {
    case one:
      document.querySelector("#displayField").value = 1;
      break;   
    case two:
      document.querySelector("#displayField").value = 2;
      break;
    case three:
      document.querySelector("#displayField").value = 3;
      break;
  }
}

我希望情况二和之后的情况显示分配的适当值而不是1。

0 个答案:

没有答案