有人可以請告知爲什麼下面的函數中的'inputValue'變量沒有被轉換爲數字。我期待第二個console.log
報告該變量現在是一個數字(因爲我申請parseInt
它)。 但顯然它仍然是一個字符串。將字符串轉換爲JS中的數字
function checkIfNum(){
var inputValue = document.getElementsByTagName('input')[0].value;
console.log(inputValue);
// Convert to a number
parseInt(inputValue);
console.log(typeof(inputValue));
}
感謝RGraham - 有一天我會得到一個程序員的大腦。 – swisstony
@swisstony嘿,我們都會犯錯!其他人更容易看到代碼中的問題,而不是看到你自己的:) – CodingIntrigue