[英]Js conditional statement not working
在提交時檢查weight變量上的非數字輸入時,我的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);
}
}
您的代碼中存在多個問題:
.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");
}
}
您可能應該使用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");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.