日期下拉菜单

<! 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=gb2312"   />
< title > 日期下拉菜单 </ title >
< script  type ="text/javascript" >
var Class = {
  create: 
function() {
    
return function() {
      
this.initialize.apply(this, arguments);
    }

  }

}


Object.extend 
= function(destination, source) {
    
for (var property in source) {
        destination[property] 
= source[property];
    }

    
return destination;
}


var DateSelector = Class.create();
DateSelector.prototype 
= {
  initialize: 
function(idYear, idMonth, idDay, options) {
    
var oYear = document.getElementById(idYear), oMonth = document.getElementById(idMonth), oDay = document.getElementById(idDay), dt = new Date(), i = 0;
    
    
this.oYear = oYear; this.oMonth = oMonth; this.oDay = oDay;
    
this.date = dt;
    
this.SetOptions(options);

    
var iYear = parseInt(this.options.year), iMonth = parseInt(this.options.month), iDay = parseInt(this.options.day);
    
this.iYear = iYear > 1900 ? iYear : dt.getFullYear() ;
    
this.iMonth = 1 <= iMonth && iMonth <= 12 ? iMonth : dt.getMonth();
    
this.iDay = iDay > 0 ? iDay : dt.getDate();

    i 
= parseInt(this.options.minyear) || 0this.minYear = i > 0 ? iYear - i : iYear;
    i 
= parseInt(this.options.maxyear) || 0this.maxYear = i > 0 ? iYear + i : iYear;
    
    
this.SetYear(); this.SetMonth(); this.SetDay();
    
    
var oDateSelector = this;
    oYear.onchange 
= function(){ oDateSelector.onYearChange() };
    oMonth.onchange 
= function(){ oDateSelector.onMonthChange() };
    oDay.onchange 
= function(){ oDateSelector.onDayChange() };
  }
,
  
//设置默认属性
  SetOptions: function(options) {
    
var dt = this.date;
    
this.options = {//默认值
      year:            dt.getFullYear(),//
      month:        dt.getMonth() + 1,//
      day:            dt.getDate(),//
      minyear:        0,//最小年份与year相差的年数
      maxyear:        5//最大年份与year相差的年数
    }
;
    Object.extend(
this.options, options || {});
  }
,
  
//年改变事件
  onYearChange: function() {
    
this.iYear = this.oYear.value; this.SetDay();
  }
,
  
//月改变事件
  onMonthChange: function() {
    
this.iMonth = this.oMonth.value; this.SetDay();
  }
,
  
//日改变事件
  onDayChange: function() {
    
this.iDay = this.oDay.value;
  }
,
  
//年设置
  SetYear: function() {
    
var minYear = this.minYear;
    
this.SetSelector(this.oYear, minYear, this.maxYear, this.iYear - minYear);
  }
,
  
//月设置
  SetMonth: function() {
    
this.SetSelector(this.oMonth, 112this.iMonth - 1);
  }
,
  
//日设置
  SetDay: function() {
    
//精髓所在
    var iDay = this.iDay, dt = new Date(this.iYear, this.iMonth, 0), daysInMonth = dt.getDate();
    
if (iDay > daysInMonth) this.iDay = iDay = daysInMonth; };
    
this.SetSelector(this.oDay, 1, daysInMonth, iDay - 1);
  }
,
  
//列表设置
  SetSelector: function(oSel, iMin, iMax, iIndex) {
    oSel.options.length 
= 0;
    
for (var i = iMin; i <= iMax; i++{
        
var op = document.createElement("OPTION");
        op.value 
= i; op.innerHTML = i;
        oSel.appendChild(op);
    }

    oSel.selectedIndex 
= iIndex;
  }

}
;
</ script >
</ head >

< body >
< select  id ="idYear" ></ select >  
< select  id ="idMonth" ></ select >  
< select  id ="idDay" ></ select >  
< script >
new DateSelector("idYear""idMonth""idDay"{ minyear:10, maxyear:10 });
</ script >
</ body >
</ html >
 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值