0
我無法通過document.getElementById從輸入字段訪問值以檢查輸入數字是偶數還是奇數。無法使用document.getElementById訪問輸入字段中的值
你能告訴我我的代碼有什麼問題嗎?
預先感謝您!
<!DOCTYPE html>
<html>
<head>
<title>Even or odd</title>
<meta charset="utf-8"/>
<link href="style.css" rel="stylesheet"/>
</head>
<body>
<form>
<p>Please type a number in the field and press the button to check if this is an even or an odd number.</p>
<input type="text" id="num" placeholder="Enter a number"/>
<button type="button" onclick="myFunction()">Click Me!</button>
</form>
<script>
var a = parseInt(document.getElementById("num").value);
function myFunction(){ if ((a/2)==%){
alert("The number is odd");
} else {
alert("The number is even");
}
}
</script>
</body>
</html>