我正在尝试使用脚本从DOB中获取用户年龄,然后根据这个年龄奖励他们分为两个区域联邦(f)和魁北克(q)移民,因为他们都计算年龄点我有一套of if ... else命令奖励积分并在两个文本框中显示。 目前用于计算点的脚本parseInt是显示年龄的文本字段(此字段由dobtoage脚本填充) 继承我的代码和live preview。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Quebec and Federal Immigration Points Calculator</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
</head>
<body>
<form name="form">
<p>Your Date of Birth (format:<strong>dd/mm/yyyy</strong>)</p><br />
<input onmouseout="showAge()" name="dob" id="dob" />
<p>Your Age</p><input name="age" type="text" style="font-size: 15px" value="" size="7" readonly>
<INPUT NAME="calc" VALUE="Calculate" TYPE="button" onClick="compute(this.form)"><br />
<input name="rsltf" type="text" style="font-size: 50px" value="" size="20" readonly /><br />
<input name="rsltq" type="text" style="font-size: 50px" value="" size="20" readonly />
</form>
<script type="text/javascript">
function showAge(){
var d =document.getElementById('dob').value.split('/');
var today=new Date();
var bday=new Date(d[2],d[1],d[0]);
var by=bday.getFullYear();
var bm=bday.getMonth()-1;
var bd=bday.getDate();
var age=0; var dif=bday;
while(dif<=today){
var dif = new Date(by+age,bm,bd);
age++;
}
age +=-2 ;
form.age.value = age;
}
</script>
<script type="text/javascript">
function compute(form)
{
var a = parseInt(form.age.value, 10) || 0;
if (a == 17)
{
f = 2; q = 0;
}
else if (a == 53)
{
f = 2; q = 0;
}
else if (17 > a > 53)
{
f = 0; q = 0;
}
else if (a == 19)
{
f = 6; q = 16;
}
else if (a == 51)
{
f = 6; q = 0;
}
else if (a == 18)
{
f = 4; q = 16;
}
else if (a == 20)
{
f = 8; q = 16;
}
else if (a == 50)
{
f = 8; q = 0;
}
else if (a == 52)
{
f = 4; q = 0;
}
else if (35 >= a >= 21)
{
f = 10; q = 16;
}
else if (a == 36)
{
f = 10; q = 14;
}
else if (a == 37)
{
f = 10; q = 12;
}
else if (a == 38)
{
f = 10; q = 10;
}
else if (a == 39)
{
f = 10; q = 8;
}
else if (a == 40)
{
f = 10; q = 6;
}
else if (a == 41)
{
f = 10; q = 4;
}
else if (a == 42)
{
f = 10; q = 2;
}
else if (a == 43)
{
f = 10; q = 0;
}
else if (50 >= a >= 44)
{
f = 10; q = 0;
}
form.rsltf.value = f;
form.rsltq.value = q;
}
</script>
</body>
</html>
答案 0 :(得分:1)
首先,你很奇怪地计算年龄。
这样做:
var today = new Date();
var bday = new Date(d[2],d[1],d[0]);
var age = today.getFullYear() - bday.getFullYear(); //If you're born in 1980 then 2011-1980 means you're about 31 years old, this will give you the age if birthday has already passed
if(today.getMonth() < bday.getMonth() || (today.getMonth() == bday.getMonth() && today.getDate() < bday.getDate()))
{
age--; //Reduce age by 1 if birthday hasn't passed
}
对于巨大的if ifif结构,首先我将其分解为两个独立的结构。这使您可以利用另一个中没有的范围来缩短整体结构。年龄范围如此混乱,我真的不想通过它来构建适当的结构。可能希望利用switch case语句而不是if / elseif / else。
此外,将您的功能放在他们所属的头部。此外,如果ShowAge()函数不是有效日期,则需要对其进行一些错误检查。此外,您需要在表单的ID上使用getElementById
在最后更改ShowAge()
中的值,否则它不知道form
是什么。因此,请为您的表单添加ID,并将ShowAge()
的结尾更改为:
document.getElementById('formid').age.value = age;