3 回答

TA貢獻1824條經驗 獲得超6個贊
我建議您閱讀更多有關html 表單的內容:
function KareAlan() {
?var kare1 = document.getElementById("kare1").value;
?var kare1n = parseInt(kare1, 10)
?document.getElementById("sonuc").innerHTML =
? ?"Result:" + " " + kare1
}
<label for="kare1">Input Number</label><br>
<input type="number" value="5" id="kare1" oninput="KareAlan()"></input>
<h3 id="sonuc"></h3>

TA貢獻1839條經驗 獲得超15個贊
使用 !isNaN(number) 檢查數字。
我們使用 isNaN(),如果不是數字則返回 true,然后我們否定該語句以獲得一個如果它是數字則返回 true 的函數。

TA貢獻1863條經驗 獲得超2個贊
正如其他人指出的那樣,該值NaN是 a number。您需要在運行代碼typeof之前進行檢查,看看會發生什么parse
var theApple = "Apple";
var theNumber = 1234;
console.log(
"log line 1: " + theApple,
parseInt(theApple, 10),
"| typeof: " + typeof(theApple),
"| typeof parsedInt: " + typeof(parseInt(theApple, 10)),
);
console.log(
"log line 2: " + theNumber,
parseInt(theNumber, 10),
"| typeof: " + typeof(theNumber),
"| typeof parsedInt: " + typeof(parseInt(theNumber, 10)),
);
// or simply
console.log("log line 3: ", !isNaN(theApple));
console.log("log line 4: ", !isNaN(theNumber));
// the problem with isNan is a value like this is also a NaN:
console.log("log line 5: ", !isNaN('0.0314E+2'));
添加回答
舉報