Javascript 这个数字脚本有什么问题?

Javascript 这个数字脚本有什么问题?,javascript,html,numbers,Javascript,Html,Numbers,我对JavaScript非常陌生,我想做一个输入检查。 这是我的剧本: function checkInp() { var x=document.forms["myForm"]["num"].value; // Get the value if (isNaN(x)) { alert("Not a number!"); // Check if the input is a number return false; } var valuex=document.f

我对JavaScript非常陌生,我想做一个输入检查。 这是我的剧本:

function checkInp()
{
  var x=document.forms["myForm"]["num"].value; // Get the value
  if (isNaN(x)) 
  {
    alert("Not a number!"); // Check if the input is a number
    return false;
  }
  var valuex=document.forms["myForm"]["num"].value; // Get the value, i don't know if i have to re-write this variable, if no, please comment.
  Number(valuex); // Make the input a number
  if (valuex.value > 480) {
    alert("Too high!"); // See if the number is greater than 480. If yes, say that, if not, return normally.
    return false;
  } 
  else {
    return;
  }
}
我不知道会发生什么,但脚本不起作用,因为我添加了第二部分(检查数字是否大于480)。

如果可能的话,请帮我举个完整的例子。

如果我没有错,我想你只需要这样做:

If(valuex > 480)..
我将这样做:

  • Document Selector查询更容易理解
  • 不要多次获得该值
  • 使用ParseInt转换数字上的变量
  • 如果成功,不要忘记返回true
代码:


你为什么要投反对票?有什么原因吗?请注意,
parseInt()
用于整数,并且它允许尾随非数字垃圾(因此接受“123hello world”)。
!0
为真,0绝对是一个数字。最好检查
isNaN(x)
x!==x
NaN
是JS中唯一一个不等于它自己的值Pontity和Thomas,你们都对了,我更新了我的代码
function checkInp() {
  var x = document.querySelector('input[name="num"]').value;  
  if (isNaN(x)) {
    alert("Must be a number");
    return false
  } else if (x > 480) {
    alert("Must be under 480");
    return false
  }
  return true
}