【发布时间】:2013-06-05 02:06:27
【问题描述】:
嘿 javascript 大师,
正在尝试为客户的网站创建年龄验证页面。下面的代码不起作用,因为无论您选择哪一年,它仍然允许您进入该站点。不知道我应该看什么来纠正。
感谢任何帮助。
<script type="text/javascript"><!--
function checkAge(f){
var dob=new Date();
var date=dob.getDate();
var month=dob.getMonth() + 1;
var year=dob.getFullYear();
var cmbmonth=parseInt(document.getElementById("cmbmonth").options[document.getElementById("cmbmonth").selectedIndex].value);
var cmbday=parseInt(document.getElementById("cmbday").options[document.getElementById("cmbday").selectedIndex].value);
var cmbyear=parseInt(document.getElementById("cmbyear").options[document.getElementById("cmbyear").selectedIndex].value);
age=year-cmbyear;
if(cmbmonth>month){age--;}
else{if(cmbmonth==month && cmbday>=date){age--;}}
if(cmbmonth==0){alert("You must enter the month you were born in.");return false;}
else if(cmbday==0){alert("You must enter the day you were born on.");return false;}
else if(cmbyear==2005){alert("You must enter the year you were born in.");return false;}
else if(age<13){alert("You are unable to view this site!");location.replace("http://www.dharmatalks.org");return false;}
else{return true;}
}
// --></script>
【问题讨论】:
-
请注意,
parseInt(document.getElementById("cmbmonth").options[document.getElementById("cmbmonth").selectedIndex].value);可以是document.getElementById("cmbmonth").value;,您的键盘不会很快磨损。 :-)