document.getElementById()。作为变量的值将不起作用

时间:2018-03-15 22:54:36

标签: javascript html

var random1 = Math.floor((Math.random() * 8));
var questionting    = document.getElementById('wagwan').value;
 
function dothething(){
  if (random1 === 0){
   document.getElementById("answer").innerHTML = "You asked:" + questionting + " And my finna woke answer is yes.";
  }
  else if (random1 === 1){
       document.getElementById("answer").innerHTML = "You asked: " + questionting +  ". My finna woke answer is nah mate.";

  }
  else if (random1 === 2){
     document.getElementById("answer").innerHTML = "You asked: "  + questionting  + ". My finna woke answer is that I dunno G.";

  }
  else if (random1 === 3){
       document.getElementById("answer").innerHTML = "You asked: " + questionting  + ". My finna woke answer is that you should try again later my B.";

  }
  else if (random1 === 4){
           document.getElementById("answer").innerHTML = "You asked: "  + questionting  + ". My finna woke answer is that it's looking good.";

  }
  else if (random1 === 5){
          document.getElementById("answer").innerHTML = "You asked: "  + questionting  + ". My finna woke answer is that it finna be.";

  }
  else if (random1 === 6){
           document.getElementById("answer").innerHTML = "You asked: "  + questionting  + ". My finna woke answer is that my finna sources say no.";

  }
  else if (random1 === 7){
          document.getElementById("answer").innerHTML = "You asked: " + questionting +  ". My finna woke answer is that you should look towards the audiovisual counsel of our overlord Shrek in this time of turbulance and malhappenings.";

  }
}
<html>
<body>
  <p1> Enter ur question and I'll give you my finna woke answer </p1> <br>  
<input id="wagwan" type="text" name="FirstName" value=""><br>
<button onclick="dothething()"> Get yo finna woke answer </button>
<p id="answer">Enter your question and we finna know whatta do<p>
</body>
</html>

我正在尝试使用JavaScript为学校项目创建一个魔法8球,但是当尝试声明var questionting = document.getElementById('wagwan').value;时,变量在调用输出时不会返回任何类型的任何值(参见图像) 。我花了一些时间试图解决这个问题,但是我找不到能够解决问题的解决方案。

Image

1 个答案:

答案 0 :(得分:3)

你宣布

var questionting    = document.getElementById('wagwan').value;

一次,还没有价值。

将它放入dosomething() - 方法并再试一次。