[英]Populate drop-down field based on birthdate
I was able to populate months, days and years. 我能够填充几个月,几天和几年。 I was also able to determine the age, which in my end I am only allowing up to age 13. 我还能够确定年龄,最终我只允许13岁以下。
Here is my code: 这是我的代码:
$('#reg-yr').change(function(){
var year = $('#reg-yr').val();
var month = $('#reg-mn').val();
var date = $('#reg-dt').val();
var firstdate=new Date(year,month,date);
var today = new Date();
var dayDiff = Math.ceil(today.getTime() - firstdate.getTime()) / (1000 * 60 * 60 * 24 * 365);
var age = parseInt(dayDiff);
});
This code checks the age when year was change. 此代码检查更改年份的年龄。 What I am having problem right now, is how to populate the month drop-down field based on the age. 我现在遇到的问题是如何根据年龄填充“月”下拉字段。
For example: The date today is June 13, 2014 例如:今天的日期是2014年6月13日
My default date in my birthday drop-down is January 1, 1980 我生日下拉菜单中的默认日期为1980年1月1日
What I wanted is for example when I change the year to 2001, the month will be populated based on maximum age. 我想要的是例如当我将年份更改为2001年时,将根据最大年龄来填充月份。
So the drop-down should be only consisting months of January down to May. 因此,下拉列表应仅包含从1月到5月的几个月。
Thank you for the help. 感谢您的帮助。
$('#reg-yr').change(function(){
var months =['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
var year = $('#reg-yr').val();
var month = $('#reg-mn').val();
var date = $('#reg-dt').val();
var today = new Date();
var i = 0;
for(i=0;i <= 12;i++)
{
var firstdate=new Date(year,i,date);
var dayDiff = Math.ceil(today.getTime() - firstdate.getTime()) / (1000 * 60 * 60 * 24 * 365);
var age = parseInt(dayDiff);
if(age < 13)
break;
}
$('#reg-mn').html('')
for(j=0;j <(i-1);j++)
{
$('#reg-mn').append('<option value="'+ j +'">'+ months[j] +'</option>') ;
}
});
See solution on js fiddel 请参阅js fiddel上的解决方案
Updated on JSFIDDLE also 还更新了JSFIDDLE
Try this... 尝试这个...
$('#reg-yr').change(function(){
var year = $('#reg-yr').val();
var month = $('#reg-mn').val();
var date = $('#reg-dt').val();
var firstdate=new Date(year,month,date);
var today = new Date();
var dayDiff = Math.ceil(today.getTime() - firstdate.getTime()) / (1000 * 60 * 60 * 24 * 365);
var age = parseInt(dayDiff);
html = '<option value="">Select Month</option>';
for(i=1;i<=age;i++) {
html += '<option value="'+i+'">'+i+'</option>';
}
$('select').html(html);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.