在提交时,在权重变量上检查非数字输入时,我的 if语句似乎无法正常工作。这是为什么?
submitBtn.onclick = function(){
var name = document.getElementById('name').value;
var weight = document.getElementById('weight').value;
var pound = weight * 2.20462;
//Check that the value is a number
if(isNan(weight)){
alert("Please enter a number);
}
}
此处位于JsFiddle link
答案 0 :(得分:3)
您的代码中存在多个问题:
.onclick
上调用submitBtn
,这是未定义的。.isNan()
.isNaN()
alert()
函数的字符串://Define submitBtn
var submitBtn = document.getElementById('submitBtn');
submitBtn.onclick = function(){
var name = document.getElementById('name').value;
var weight = document.getElementById('weight').value;
var pound = weight * 2.20462;
//Call isNaN()
if(isNaN(weight)){
//Close your string
alert("Please enter a number");
}
}
答案 1 :(得分:1)
您可以使用isNaN
submitBtn.onclick = function(){
var name = document.getElementById('name').value;
var weight = document.getElementById('weight').value;
var pound = weight * 2.20462;
//Check that the value is a number
if(isNaN(weight)){
alert("Please enter a number");
}
}