Implementation Code of date association based on JQuery

Source: Internet
Author: User

Goals:

There are two dates in the drop-down box:
Copy codeThe Code is as follows:
<Head>
// Import the jquery address
<Script src = "(Jquery address)" language = "JavaScript" type = "text/javascript"> </script>
<Script type = "text/javascript">
// StartYear changed
Function changeYear (str, isstart)
{
Var pre = "start ";
If (isstart = false)
{
Pre = "end ";
}
Var startMonth = $ (pre + "Month"). value;
If (startMonth = "")
{
Var e = $ (pre + "Month ");
OptionClear (e );
Return;
}
Var n = MonHead [startMonth-1];
If (startMonth = 2 & IsPinYear ($ (pre + "Year"). value ))
{
N ++;
}
WriteDay (n, pre );
}
Function changeMonth (str, isstart)
{
Var pre = "start ";
If (isstart = false)
{
Pre = "end ";
}
Var year = $ (pre + "Year"). value;
If (year = "")
{
Var e = $ (pre + "Day ");
OptionClear (e );
Return;
}
Var n = MonHead [str-1];
If (str = 2 & IsPinYear ($ (pre + "Year ")))
{
N ++;
}
WriteDay (n, pre );
}
Function dateStart ()
{
Var defastarstarty = "2011 ";
Var defastarstartm = "1 ";
Var defastarstartd = "14 ";
Var defaultEndY = "2011 ";
Var defaultEndM = "2 ";
Var defaendendd = "23 ";
MonHead = [, 31,];
Var prestr = new Array ("start", "end ");
For (var j = 0; j <2; j ++)
{
Var pre = prestr [j];
// Start year
Var y = new Date (). getFullYear ();
If (pre = "start ")
{
// 10th days before start
Var I _index = 0;
For (var I = (y-10); I <= y; I ++)
{
$ (Pre + "Year"). options. add (new Option ("" + I + "", I ));
If (I = defaultStartY)
{
$ (Pre + "Year"). options [I _index]. selected = true;
}
I _index ++;
}
}
Else
{
Var I _index = 0;
For (var I = (y-5); I <= y + 5; I ++)
{
$ (Pre + "Year"). options. add (new Option ("" + I + "", I ));
If (I = defaultEndY)
{
$ (Pre + "Year"). options [I _index]. selected = true;
}
I _index ++;
}
}
// Start month
DefaultM = (pre = "start "? DefaultStartM: defaultEndM)
For (var I = 1; I <13; I ++)
{
$ (Pre + "Month"). options. add (new Option ("" + I + "", I ));
If (I = defaultM)
{
$ (Pre + "Month"). options [I-1]. selected = true;
}
}
// Start day
Var n = MonHead [$ (pre + "Month"). value];
If (new Date (). getMonth = 1 & IsPinYear ($ (pre + "Year"). value ))
{
N ++;
}
DefaultD = (pre = "start "? DefaultStartD: defaendendd)
WriteDay (n, pre );
$ (Pre + "Day"). options [defaultD-1]. selected = true;
}
}
Function writeDay (n, pre)
{
Var e = $ (pre + "Day ");
OptionClear (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 % 4 = 0 ));
}
Function optionClear (e)
{
For (var I = e. options. length; I> = 0; I --)
{
E. remove (I );
}
}
</Script>
</Head>
<Body onload = "dateStart ()">
<P name = "selectdate">
Slave
<Select id = "startYear" name = "startYear" onchange = "changeYear (this. value, true)">
</Select>
Year
<Select id = "startMonth" name = "startMonth" onchange = "changeMonth (this. value, true)">
</Select>
Month
<Select id = "startDay" name = "startDay">
</Select>
Day
To
<Select id = "endYear" name = "endYear" onchange = "changeYear (this. value, false)">
</Select>
Year
<Select id = "endMonth" name = "endMonth" onchange = "changeMonth (this. value, false)">
</Select>
Month
<Select id = "endDay" name = "endDay">
</Select>
Day
</P>
</Body>

I have referenced some code and hope to help some friends who need it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.