您好,欢迎访问一九零五行业门户网

怎样利用源生JS实现出生日期下拉菜单

这次给大家带来怎样利用源生js实现出生日期下拉菜单,利用源生js实现出生日期下拉菜单的注意事项有哪些,下面就是实战案例,一起来看一下。
在制作网页时,可能需要给用户提供注册帐号页面,用户注册 设计很多信息,其中就有 关于出生日期的,出于用户体验,不想让用户手动输入,而html5的date,目前很多浏览器支持的并不是很好,所以就可以用js实现年、月、日3个下拉框的日期选择。具体代码如下:
1、新建一个js文件,如birthday.js;
function dateselector(selyear, selmonth, selday) {//定义函数   this.selyear = selyear;   this.selmonth = selmonth;   this.selday = selday;   this.selyear.group = this;   this.selmonth.group = this; // 给年份、月份下拉菜单添加处理onchange事件的函数   if (window.document.all != null) // ie   {     this.selyear.attachevent(onchange, dateselector.onchange);     this.selmonth.attachevent(onchange, dateselector.onchange);   }   else // firefox   {     this.selyear.addeventlistener(change, dateselector.onchange, false);     this.selmonth.addeventlistener(change, dateselector.onchange, false);   }   if (arguments.length == 4) // 如果传入参数个数为4,最后一个参数必须为date对象     this.initselector(arguments[3].getfullyear(), arguments[3].getmonth() + 1, arguments[3].getdate());   else if (arguments.length == 6) // 如果传入参数个数为6,最后三个参数必须为初始的年月日数值     this.initselector(arguments[3], arguments[4], arguments[5]);   else // 默认使用当前日期   {     var dt = new date();     this.initselector(dt.getfullyear(), dt.getmonth() + 1, dt.getdate());   } } // 增加一个最小年份的属性--最老年份 dateselector.prototype.minyear = 1960; // 增加一个最大年份的属性--最新年份,即当前时期getfullyear() dateselector.prototype.maxyear = (new date()).getfullyear(); // 初始化年份 dateselector.prototype.inityearselect = function () { // 循环添加opion元素到年份select对象中   for (var i = this.maxyear; i >= this.minyear; i--) { // 新建一个option对象     var op = window.document.createelement(option); // 设置option对象的值     op.value = i; // 设置option对象的内容     op.innerhtml = i; // 添加到年份select对象     this.selyear.appendchild(op);   } } // 初始化月份 dateselector.prototype.initmonthselect = function () { // 循环添加opion元素到月份select对象中   for (var i = 1; i < 13; i++) { // 新建一个option对象 var op = window.document.createelement("option"); // 设置option对象的值 op.value = i; // 设置option对象的内容 op.innerhtml = i; // 添加到月份select对象 this.selmonth.appendchild(op); } } // 根据年份与月份获取当月的天数 dateselector.daysinmonth = function (year, month) { var date = new date(year, month, 0); return date.getdate(); } // 初始化天数 dateselector.prototype.initdayselect = function () { // 使用parseint函数获取当前的年份和月份 var year = parseint(this.selyear.value); var month = parseint(this.selmonth.value); // 获取当月的天数 var daysinmonth = dateselector.daysinmonth(year, month); // 清空原有的选项 this.selday.options.length = 0; // 循环添加opion元素到天数select对象中 for (var i = 1; i <= daysinmonth; i++) { // 新建一个option对象 var op = window.document.createelement("option"); // 设置option对象的值 op.value = i; // 设置option对象的内容 op.innerhtml = i; // 添加到天数select对象 this.selday.appendchild(op); } } // 处理年份和月份onchange事件的方法,它获取事件来源对象(即selyear或selmonth) // 并调用它的group对象(即dateselector实例,请见构造函数)提供的initdayselect方法重新初始化天数 // 参数e为event对象 dateselector.onchange = function (e) { var selector = window.document.all != null ? e.srcelement : e.target; selector.group.initdayselect(); } // 根据参数初始化下拉菜单选项 dateselector.prototype.initselector = function (year, month, day) { // 由于外部是可以调用这个方法,因此我们在这里也要将selyear和selmonth的选项清空掉 // 另外因为initdayselect方法已经有清空天数下拉菜单,因此这里就不用重复工作了 this.selyear.options.length = 0; this.selmonth.options.length = 0; // 初始化年、月 this.inityearselect(); this.initmonthselect(); // 设置年、月初始值 this.selyear.selectedindex = this.maxyear - year; this.selmonth.selectedindex = month - 1; // 初始化天数 this.initdayselect(); // 设置天数初始值 this.selday.selectedindex = day - 1; }
2、在注册表单中的页面,引用刚才写的js
<script type="text/javascript" src="/js/birthday.js"></script> <select id="selyear"></select>年 <select id="selmonth"></select>月 <select id="selday"></select>日 <!--完成出生日期的选择--需写在</body>前--> <script type="text/javascript"> var selyear = window.document.getelementbyid(selyear); var selmonth = window.document.getelementbyid(selmonth); var selday = window.document.getelementbyid(selday); // 新建一个dateselector类的实例,将三个select对象传进去 new dateselector(selyear, selmonth, selday, 1995, 1, 17); </script> </body> </html>
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
如何使用express托管静态文件
使用js实做出加密解密操作
以上就是怎样利用源生js实现出生日期下拉菜单的详细内容。
其它类似信息

推荐信息