欢迎各位兄弟 发布技术文章
这里的技术是共享的
下面是html代码 ,, <select name=YYYY onchange="YYYYMM(this.value)" class="selectyear"></select> | |
<select name=MM onchange="MMDD(this.value)" class="selectmonth"></select> | |
<select name=DD class="selectday"></select> | |
下面是js代码 ,把下面的代码保存为一个js文件就好了,当然要先引入jquery | |
jQuery.fn.outerHTML = function(s) { return s ? this.before(s).remove() : jQuery("<p id='abcde'>").append(this.eq(0).clone()).html(); }; $(document).ready(function(){ //strYYYY = document.addcontentZsjz.YYYY.outerHTML; //strMM = document.addcontentZsjz.MM.outerHTML; //strDD = document.addcontentZsjz.DD.outerHTML; strYYYY = $("[name='YYYY']").outerHTML(); strMM = $("[name='MM']").outerHTML(); strDD = $("[name='DD']").outerHTML(); MonHead = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; //先给年下拉框赋内容 var y = new Date().getFullYear(); var str = strYYYY.substring(0, strYYYY.length - 9); for (var i = (y-64); i < (y+10); i++) //以今年为准,前30年,后30年 { str += "<option value='" + i + "'> " + i + "</option>\r\n"; } str +="</select>"; //document.addcontentZsjz.YYYY.outerHTML = str +"</select>"; $("[name='YYYY']").outerHTML(str); //赋月份的下拉框 var str = strMM.substring(0, strMM.length - 9); for (var i = 1; i < 13; i++) { str += "<option value='" + i + "'> " + i + "</option>\r\n"; } //document.addcontentZsjz.MM.outerHTML = str +"</select>"; str +="</select>"; $("[name='MM']").outerHTML(str); //document.addcontentZsjz.YYYY.value = y; $("[name='YYYY']").val(y); //document.addcontentZsjz.MM.value = new Date().getMonth() + 1; $("[name='MM']").val(new Date().getMonth() + 1); var n = MonHead[new Date().getMonth()]; YYYYvalue = y; if (new Date().getMonth() ==1 && IsPinYear(YYYYvalue)) n++; writeDay(n); //赋日期下拉框 //document.addcontentZsjz.DD.value = new Date().getDate(); $("[name='DD']").val(new Date().getDate()); }); function YYYYMM(str) //年发生变化时日期发生变化(主要是判断闰平年) { var MMvalue = $("[name='MM']").val(); if (MMvalue == ""){DD.outerHTML = strDD; return;} var n = MonHead[MMvalue - 1]; if (MMvalue ==2 && IsPinYear(str)) n++; writeDay(n) } function MMDD(str) //月发生变化时日期联动 { var YYYYvalue = $("[name='YYYY']").val(); if (str == ""){DD.outerHTML = strDD; return;} var n = MonHead[str - 1]; if (str ==2 && IsPinYear(YYYYvalue)) n++; writeDay(n) } function writeDay(n) //据条件写日期的下拉框 { var s = strDD.substring(0, strDD.length - 9); for (var i=1; i<(n+1); i++) s += "<option value='" + i + "'> " + i + "</option>\r\n"; //document.addcontentZsjz.DD.outerHTML = s +"</select>"; s +="</select>"; $("[name='DD']").outerHTML(s); } function IsPinYear(year)//判断是否闰平年 { return(0 == year%4 && (year%100 !=0 || year%400 == 0)) } | |