(function($) {
$.fn.birthday = function(options) {
var opts = $.extend({}, $.fn.birthday.defaults, options); //整合参数
var $year = $(this).children("select[name=" + opts.year + "]");
var $month = $(this).children("select[name=" + opts.month + "]");
var $day = $(this).children("select[name=" + opts.day + "]");
MonHead = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
return this.each(function() {
var y = new Date().getFullYear();
var con = "";
//添加年份
for (i = y; i <= parseInt(y+1); i++) {
con += "" + i + "年" + "";
}
$year.append(con);
var m = new Date().getMonth()+1;
con1 = "";
con2 = "";
con3 = "";
//添加月份
for (i = m; i <=12; i++) {
con1 += "" + i + "月" + "";
}
for (i = 1; i
con2 += "" + i + "月" + "";
}
for (i = 1; i <12; i++) {
con3 += "" + i + "月" + "";
}
con = con1 +con2;
$month.append(con);
$('.year').change(function(){
if($(this).attr('data-current-year') == $(this).val()){
console.log(1)
$month.append(con3);
}
});
con = "";
//添加日期
var n = MonHead[0]; //默认显示第一月
var d = new Date().getDate();
for (i = d; i <= n; i++) {
con += "" + i + "日" + "";
}
$day.append(con);
$.fn.birthday.change($(this));
console.log(y,m,d,$year[name='']);
});
};
$.fn.birthday.change = function(obj) {
obj.children("select[name=" + $.fn.birthday.defaults.year + "],select[name=" + $.fn.birthday.defaults.month + "]").change(function() {
var $year = obj.children("select[name=" + $.fn.birthday.defaults.year + "]");
var $month = obj.children("select[name=" + $.fn.birthday.defaults.month + "]");
var $day = obj.children("select[name=" + $.fn.birthday.defaults.day + "]");
$day.empty();
var selectedYear = $year.find("option:selected").val();
var selectedMonth = $month.find("option:selected").val();
if (selectedMonth == 2 && $.fn.birthday.IsRunYear(selectedYear)) { //如果是闰年
var c = "";
for (var i = 1; i <= 29; i++) {
c += "" + i + "日" + "";
}
$day.append(c);
} else { //如果不是闰年也没选2月份
var c = "";
for (var i = 1; i <= MonHead[selectedMonth - 1]; i++) {
c += "" + i + "日" + "";
}
$day.append(c);
}
});
};
$.fn.birthday.IsRunYear = function(selectedYear) {
return (0 == selectedYear % 4 && (selectedYear % 100 != 0 || selectedYear % 400 == 0));
};
$.fn.birthday.defaults = {
year: "year",
month: "month",
day: "day"
};
})(jQuery);
$("#birthday_container").birthday();