Number from input box Javascript

后端 未结 4 503
没有蜡笔的小新
没有蜡笔的小新 2021-01-28 12:02

I\'m trying to get a number from an input box, but it returns an empty string as an alert. What do I miss?

4条回答
  •  孤城傲影
    2021-01-28 12:51

    You need to read the value of the input inside the function, not during page load. The following code will work.

    var button = document.getElementsByClassName("btn")[0];
    
    button.addEventListener("click", calculate);
    
    function calculate() {
      var num = document.getElementById("input").value;
      alert(num);
    }
    

提交回复
热议问题