我想检查用户输入的数字是否; “输入”来自html输入标签。我在下面提供了代码。我不明白为什么我的代码无法正常工作,我们将不胜感激。

document.getElementById('input').addEventListener("input",
function(){
  var userInput = document.getElementById('input').value;
  if(!isNaN(userInput)){
    alert("Is a number");
  }
  else{
    alert("Is not a number");
  }

},true);
User Input : <input id="input">

最佳答案

你可以使用typeof

var userInput = document.getElementById('input').value;
if(typeof userInput == 'number')){
  console.log("Is a number");
}else{
  console.log("Is not a number");
}

09-26 21:51
查看更多