<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta http-equiv="Content-Language" content="zh-CN">
<title>DronFw Example</title>
<script language="javascript">
///*******************************************\
//  游戏人日期选择类(2006-06-27)
//  This JavaScript was writen by Dron.
//  @2003-2008 Ucren.com All rights reserved.
//\*******************************************/
</script>
<style>
body,td{font-size:12px;}
</style>
</head><body>
<script language="javascript">
///*******************************************\
//  游戏人日期选择类(2006-06-27)
//  This JavaScript was writen by Dron.
//  @2003-2008 Ucren.com All rights reserved.
//\*******************************************/</script>
<h2>游戏人日期输入类</h2>
<hr>选择日期:<input type="text" id="abc">
选择日期:<input type="text" id="abc2"><script language="javascript">
var Calendar = function (obj)
{
var o = this;
var nowIs = new Date();
var yy = nowYear = nowIs.getFullYear();
var mm = nowMonth = nowIs.getMonth() + 1;
var dd = nowDate = nowIs.getDate();
o.Element = window.document.getElementById(obj);
o.Element.onfocus = function ()
{
o.DivObject.style.left = this.offsetLeft + "px";
o.DivObject.style.top = this.offsetTop + this.offsetHeight + "px";
o.DivObject.style.display = "block";
}
o.getDaysCount = function (yea,mon){return (new Date(yea,mon,0)).getDate();}
o.getFirstDay = function (yea,mon){return (new Date(yea,mon-1,1)).getDay();}
o.innerHTMLToTd = function (n,str)
{
var tds = o.DivObject.getElementsByTagName("td");
tds[n+8].innerHTML = str;
tds[n+8].onclick = (str=="&nbsp;")?null:function ()
{
dd = this.innerHTML.replace(/[^\d]/g,"");
o.Element.value = yy + "-" + mm + "-" + dd;
o.DivObject.style.display = "none";
};
}
o.showDayLists = function (yea,mon)
{
var ms = o.getDaysCount(yea,mon);
var fd = o.getFirstDay(yea,mon);
for(var i=0;i<42;i++)
o.innerHTMLToTd(i,"&nbsp;");
for(var i=1;i<=ms;i++)
o.innerHTMLToTd(i+fd-1,((nowYear==yea&&nowMonth==mon&&nowDate==i)?("<font color=\"red\">"+i+"</b>"):i));
}
o.init = function ()
{
var s = "<table cellspacing=\"1\" cellpadding=\"0\" border=\"0\" width=\"148\" height=\"161\" bgcolor=\"#EFEAE1\" style=\"cursor:default;width:150px;height:163px;border-left:2px solid #e2e0da;border-top:2px solid #e2e0da;border-right:2px solid #878583;border-bottom:2px solid #878583;\">\
<tr height=\"20\" bgcolor=\"#A19E98\">\
<td colspan=\"7\" align=\"center\">\
<select style=\"width:70px;\" _DronFw_Calendar=\"true\">";
for(var i=nowYear-50;i<=nowYear+50;i++)
s = s.concat("<option" + (i==nowYear?" selected=\"selected\"":"") + " value=\"" +i+ "\">" +i+ "年</option>");
s = s.concat("</select><select style=\"width:50px;\" _DronFw_Calendar=\"true\">");
for(var i=1;i<=12;i++)
s = s.concat("<option" +(i==nowMonth?" selected=\"selected\"":"")+ " value=\"" +i+ "\">" +i+ "月</option>");
s = s.concat("</select><input onclick=\"this.parentNode.parentNode.parentNode.parentNode.parentNode.style.display=\'none\'\" type=\"button\" value=\"×\" style=\"border:1px;width:18px;height:20px;\">\
</td>\
</tr>\
<tr height=\"20\" bgcolor=\"#A19E98\" align=\"center\">");
for(var i=0;i<7;i++)
s = s.concat("<td width=\"20\">" +"日一二三四五六".charAt(i)+ "</td>");
s = s.concat("</tr>");
for(var i=0;i<6;i++)
{
s = s.concat("<tr height=\"20\" bgcolor=\"#D4D0C8\" align=\"center\">\
<td width=\"20\" bgcolor=\"#C6C2BB\" onmouseover=\"this.style.backgroundColor=\'#ffffff\'\" onmouseout=\"this.style.backgroundColor=\'\'\"></td>");
for(var j=0;j<5;j++)
s = s.concat("<td width=\"20\" onmouseover=\"this.style.backgroundColor=\'#ffffff\'\" onmouseout=\"this.style.backgroundColor=\'\'\"></td>");
s = s.concat("<td width=\"20\" bgcolor=\"#C6C2BB\" onmouseover=\"this.style.backgroundColor=\'#ffffff\'\" onmouseout=\"this.style.backgroundColor=\'\'\"></td>\
</tr>");
}
s = s.concat("</table>");
o.DivObject = window.document.createElement("div");
o.DivObject.style.position = "absolute";
o.DivObject.style.display = "none";
window.document.body.appendChild(o.DivObject);
o.DivObject.innerHTML = s;
o.showDayLists(yy,mm);
var sels = o.DivObject.getElementsByTagName("select");
sels[0].onchange = function (){o.showDayLists(yy=this.value,mm);}
sels[1].onchange = function (){o.showDayLists(yy,mm=this.value);}
}
o.init();
}
new Calendar("abc");
new Calendar("abc2");/*
类 Calendar 不需要属性和方法。
*/
</script></body>
</html>

解决方案 »

  1.   

    <html>
    <head>
    <title>年月日三下拉框联动</title>
    <meta http-equiv='Content-Type' content='text/html; charset=gb2312'>
    <meta name='author' content='F.R.Huang(meizz梅花雪)//www.meizz.com'>
    </head><body>
    <form name="form1">
      <select name="YYYY" onchange="YYYYDD(this.value)">
        <option value="">请选择 年</option>
      </select>
      <select name="MM"   onchange="MMDD(this.value)">
        <option value="">选择 月</option>
      </select>
      <select name="DD">
        <option value="">选择 日</option>
      </select>
    </form><script language="JavaScript"><!--
    function YYYYMMDDstart()
    {
        MonHead = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];    //先给年下拉框赋内容
        var y   = new Date().getFullYear();
        for (var i = (y-30); i < (y+30); i++) //以今年为准,前30年,后30年
            document.form1.YYYY.options.add(new Option(" "+ i +" 年", i));    //赋月份的下拉框
        for (var i = 1; i < 13; i++)
            document.form1.MM.options.add(new Option(" " + i + " 月", i));    document.form1.YYYY.value = y;
        document.form1.MM.value = new Date().getMonth() + 1;
        var n = MonHead[new Date().getMonth()];
        if (new Date().getMonth() ==1 && IsPinYear(YYYYvalue)) n++;
            writeDay(n); //赋日期下拉框Author:meizz
        document.form1.DD.value = new Date().getDate();
    }
    if(document.attachEvent)
      window.attachEvent("onload", YYYYMMDDstart);
    else
      window.addEventListener('load', YYYYMMDDstart, false);
    function YYYYDD(str) //年发生变化时日期发生变化(主要是判断闰平年)
    {
        var MMvalue = document.form1.MM.options[document.form1.MM.selectedIndex].value;
        if (MMvalue == ""){ var e = document.form1.DD; optionsClear(e); return;}
        var n = MonHead[MMvalue - 1];
        if (MMvalue ==2 && IsPinYear(str)) n++;
            writeDay(n)
    }
    function MMDD(str)  //月发生变化时日期联动
    {
        var YYYYvalue = document.form1.YYYY.options[document.form1.YYYY.selectedIndex].value;
        if (YYYYvalue == ""){ var e = document.form1.DD; optionsClear(e); return;}
        var n = MonHead[str - 1];
        if (str ==2 && IsPinYear(YYYYvalue)) n++;
            writeDay(n)
    }
    function writeDay(n)  //据条件写日期的下拉框
    {
        var e = document.form1.DD; optionsClear(e);
        for (var i=1; i<(n+1); i++)
            e.options.add(new Option(" "+ i + " 日", i));
    }
    function IsPinYear(year)//判断是否闰平年
    {   return(0 == year%4 && (year%100 !=0 || year%400 == 0));}
    function optionsClear(e)
    {
        e.options.length = 1;
    }
    //--></script>
    </body>
    </html>