這是我的代碼,用於輸入他們的生日來計算一個人的年齡。我無法讀取生日輸入並在getAge函數中使用。此外,我無法在第二個文本框中顯示年齡。我不確定如何糾正這些。HTML按鈕沒有運行JavaScript函數和函數輸出不顯示
<!DOCTYPE html>
<html lang="en">
<head>
<title>Age Calculator</title>
<script>
var birthday;
function getAge(birth) {
var bday = document.getElementById("bday");
var ageDisplay = document.getElementById("ageDisplay");
var today = new Date();
var curr_date = today.getDate();
var curr_month = today.getMonth() + 1;
var curr_year = today.getFullYear();
var pieces = birth.split('/');
var birth_date = pieces[0];
var birth_month = pieces[1];
var birth_year = pieces[2];
if (curr_month == birth_month && curr_date >= birth_date) return parseInt(curr_year - birth_year);
if (curr_month == birth_month && curr_date < birth_date) return parseInt(curr_year - birth_year - 1);
if (curr_month > birth_month) return parseInt(curr_year - birth_year);
if (curr_month < birth_month) return parseInt(curr_year - birth_year - 1);
}
var age = getAge(birthday);
ageDisplay.value = age
</script>
</head>
<body style="background-color:red">
<form>
<fieldset>
<label>Type your Date of Birth: </label>
<input type="text" id="bday" />
<input type="button" value="click me" onclick="getAge()" />
<br/>
<br/>
<label>Your Age is: </label>
<input type="text" id="ageDisplay" />
</fieldset>
</form>
<body>
</html>