我有两个字段。第一个是名称字段,另一个是数字字段。我想检查数字字段是否为数字,如果不是,则将使用 select() 并且背景变为黄色而不是蓝色。不知何故,我没有得到我想要的。
window.onload=function()
{
idUser = document.getElementById("user");
document.getElementById("form").onsubmit=function()
{
if(idUser.value == "")
{
alert("Username Required");
idUser.focus();
return false;
}
birYear = document.getElementById("birthyear");
if(isNaN(birYear))
{
birYear.select();
alert("number");
return false;
}
}
}
我正在使用 alert("number");
只是为了仔细检查它是否正确执行。我不需要警报,但不知何故,如果我以两种方式输入数字或文本,它只是警报数字,这意味着我没有做我想做的事情。
在此先感谢人们帮助我。
最佳答案
你必须获得值(value):
birYear = document.getElementById("birthyear").value;
document.getElementById("birthyear")
让您控制在 document.getElementById("birthyear").value
中给出该输入的值(或任何其他)控制..
和isNan()
是为了值(value)而不是控制....
编辑:
您可以通过更改样式来做到这一点:document.getElementById("birthyear").style.background-color = "yellow";
关于javascript - 如何在这里正确使用 isNaN?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15541434/