尝试从 DOB 导出年龄,然后使用 if..else 语句计算分数

发布于 2024-12-22 10:16:12 字数 2926 浏览 0 评论 0原文

我正在尝试使用脚本从 DOB 得出用户年龄,然后根据这个年龄为他们授予联邦(f)和魁北克(q)移民两个领域的积分,因为它们都计算年龄积分,我有一组 if 。 ..else 命令奖励积分并将其显示在两个文本框中。 目前用于计算点的脚本 parseInt 是显示年龄的文本字段(该字段由 dobtoage 脚本填充) 这是我的代码和实时预览

<!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>

I'm trying to derive the users age from DOB using a script, then depending on this age award them points for two areas Federal(f) and Quebec(q) Immigration, as they both calculate age points i have a set of if...else commands to award points and display them in two textboxes.
currently the script for calcualting the points parseInt's the text field that displays the age(this field is populated by the dobtoage script)
heres my code and a 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

久夏青 2024-12-29 10:16:12

首先,你计算年龄的方式很奇怪。

这样做:

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 elseif 结构,首先我会将其分解为两个独立的结构。这使您可以利用其中一个范围中另一个范围中不可用的优势来缩短整体结构。其中的年龄范围是如此混乱,以至于我真的不想通过它来构建适当的结构。可能想要利用 switch case 语句而不是 if/elseif/else。

另外,将你的函数放在它们所属的头部。另外,您需要对 ShowAge() 函数进行一些错误检查,以防它不是有效日期。另外,您需要在表单的 id 上使用 getElementById 来更改其最后在 ShowAge() 中的值,否则它不知道 form 是什么是。因此,为您的表单提供一个 id 并将 ShowAge() 的末尾更改为:

document.getElementById('formid').age.value = age; 

First, you're calculating age weirdly.

Do this:

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
}

As to the giant if elseif structure, first I'd break it up into two separate structures. This allows you to take advantage of ranges in one that aren't available in the other to shorten your overall structure. The age ranges are so jumbled up in it that I don't really feel like picking through it to construct the appropriate structures. Might want to take advantage of switch case statements instead of if/elseif/else.

Also, put your functions up in the head where they belong. Also, you need some error checking on the ShowAge() function in case it's not a valid date. Also, you need use getElementById on the form's id to change it's value in ShowAge() at the end, otherwise it has no idea what form is. So, give your form an id and change the end of ShowAge() to:

document.getElementById('formid').age.value = age; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文