jquery根据出生日期获取年龄及星座

该博客介绍了如何在JSP页面中利用jQuery,根据用户输入的出生日期来动态计算其年龄和对应的星座信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 JSP页面

<tr>
    <td style="width:15%;text-align:right;"><span style="color:red;margin-right: 5px">*</span>生日:</td>
    <td><input type="date" class="textBox length-middle"   name="birthday" id="birthday"  value="" onblur="countAgeandconstellation()"/></td>
</tr>
<tr>
    <td style="text-align:right;">年龄:</td>
    <td><input type="text" class="textBox length-middle"  id="ageid" name="age"  readonly="readonly" /></td>
</tr>
<tr>
    <td style="width:15%;text-align:right;">星座:</td>
    <td><input type="text" class="textBox length-middle"  id="constellationid" name="constellation" readonly="readonly" /></td>
</tr>

 

 根据出生日期获取年龄

function byage(strBirthday ){
	var returnAge;  
	var strBirthdayArr=strBirthday.split("-");  
        var birthYear = strBirthdayArr[0];  
        var birthMonth = strBirthdayArr[1];  
        var birthDay = strBirthdayArr[2];  
        d = new Date();  
        var nowYear = d.getFullYear();  
	var nowMonth = d.getMonth() + 1;  
	var nowDay = d.getDate();  
	if(nowYear == birthYear){  
	  returnAge = 0;//同年 则为0岁  
	}  
	else{  
	var ageDiff = nowYear - birthYear ; //年之差  
		if(ageDiff > 0){  
		if(nowMonth == birthMonth) {  
		var dayDiff = nowDay - birthDay;//日之差  
		if(dayDiff < 0)  
		returnAge = ageDiff - 1;  
		else  
		returnAge = ageDiff ;  
		}  
		else  
		{  
		var monthDiff = nowMonth - birthMonth;//月之差  
		if(monthDiff < 0)  
		returnAge = ageDiff - 1;  
		else  
		returnAge = ageDiff ;  
		}  
		}  
		else  
		returnAge = -1;//返回-1 表示出生日期输入错误 晚于今天  
		}  
		return returnAge;//返回周岁年龄
	}
    

根据出生日期获取星座

function getconstellation() {
         var birthday=new Date($("#birthday").val());
          var month=birthday.getMonth()+1;
	  var day=birthday.getDate();
    	  var d = new Date(1999, month - 1, day, 0, 0, 0);
    	  var arr = [];
    	  arr.push(["魔羯座", new Date(1999, 0, 1, 0, 0, 0)])
    	  arr.push(["水瓶座", new Date(1999, 0, 20, 0, 0, 0)])
    	  arr.push(["双鱼座", new Date(1999, 1, 19, 0, 0, 0)])
    	  arr.push(["牡羊座", new Date(1999, 2, 21, 0, 0, 0)])
    	  arr.push(["金牛座", new Date(1999, 3, 21, 0, 0, 0)])
    	  arr.push(["双子座", new Date(1999, 4, 21, 0, 0, 0)])
    	  arr.push(["巨蟹座", new Date(1999, 5, 22, 0, 0, 0)])
    	  arr.push(["狮子座", new Date(1999, 6, 23, 0, 0, 0)])
    	  arr.push(["处女座", new Date(1999, 7, 23, 0, 0, 0)])
    	  arr.push(["天秤座", new Date(1999, 8, 23, 0, 0, 0)])
    	  arr.push(["天蝎座", new Date(1999, 9, 23, 0, 0, 0)])
    	  arr.push(["射手座", new Date(1999, 10, 22, 0, 0, 0)])
    	  arr.push(["魔羯座", new Date(1999, 11, 22, 0, 0, 0)])
    	  for (var i = arr.length - 1; i >= 0; i--) {
    	  if (d >= arr[i][1]) return arr[i][0];
    	  }
    	 }

获取

function countAgeandconstellation(){
    	var age=byage($("#birthday").val());
    	var constellation=getconstellation();
    	$("#ageid").val(age);
    	$("#constellationid").val(constellation);
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值