當我輸入輸入字段中的值時,我想查找該人是否小於18歲。爲此,我創建了一個日期輸入框並創建了一個函數。我的功能無法正常工作。我做錯了什麼,我該如何做這項工作?使用JQuery查找一個人是否年齡在18歲以下
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Birthday Validation</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$(document).ready(function() {
$("#DOB").on("change paste keyup", function() {
$datestring = $(this).val();
//alert(jQuery.type(Date($datestring)));
//$eightYearsAgo = now() - $birthday;
$birthday = $.datepicker.formatDate('yy/mm/dd', new Date()) - Date($datestring);
if(!birthday.isValid()){
alert("INVALID DATE");
}else if (eightYearsAgo.isAfter(birthday)){
alert("18+");
}else{
alert("< 18");
}
});
});
</script>
</head>
<body>
<input name="DOB" type="date" class="form-control" id="DOB" required placeholder="MM/DD/YYYY" onchange="validate(date)">
</body>
</html>
你可能得爲此過濾 –
「兩個日期之間的」設置瀏覽器的開發控制檯是告訴你,你試圖使用你從未定義過的變量。 – David
你沒有一個,但很多你所引用的變量和函數不存在.. –