Simple jquery Date Selection

Source: Internet
Author: User

Create a text box that holds the date

Click the text box to pop up a selection box
referencing jquery files
Referencing bootstrap files

Using modal boxes

Using modal box method settings Click the text box to display the modal box
To manually open the modal box:
$ (' id '). Modal (' show ');
Off: (Automatically closes when you click OK)
$ (' id '). Modal (' hide ')
Populate data, create an external JS file

JS Method:

Load year
function Loadnian ()
{
var date=new date;
var year=date.getfullyear ();

var str = "";

for (Var i=year-5;i<year+6;i++) for loop sets the first five years of the current year to the last five years
{
if (i==year) locates the current year
{
str + = "<option selected= ' selected ' value= '" +i+ ">" +i+ "</option>";
}
Else
{
str + = "<option value=" "+i+" > "+i+" </option> ";
}
}

$ ("#nian"). html (str);


}

Load Month
Function Loadyue ()
{
var date=new date;
var yue=date.getmonth () +1;

var str = "";

for (var i=1;i<13;i++)
{
if (I==yue) locates the current month
{
str + = "<option selected= ' selected ' value= '" +i+ ">" +i+ "</option>";
}
Else
{
str + = "<option value= '" +i+ ">" +i+ "</option>";
}
}
$ ("#yue"). html (str);
}

Load days
function Loadtian ()
{
var date=new date;
var Tian = date.getdate ();

var zs = 31; Total days
var Nian = $ ("#nian"). Val (); Take the current selected year and month
var Yue = $ ("#yue"). Val ();
if (Yue = = 4 | | yue==6 | | yue==9 | | yue==11) Judging if it's 4, 6, 9, November that's 30 days.
{
ZS = 30;
}
else if (yue==2) judge if it is February to continue judging the year
{
if ((nian%4==0 && nian%100!=0) | | nian%400==0) is divisible by four and cannot be divisible by 100 as a leap year
{
ZS = 29;
}
Else
{
ZS = 28;
}
}

var str = "";

for (Var i=1;i<zs+1;i++)
{
if (I==tian) locates the current day
{
str + = "<option selected= ' selected ' value= '" +i+ ">" +i+ "</option>";
}
Else
{
str + = "<option value=" "+i+" > "+i+" </option> ";
}
}

$ ("#tian"). html (str);

}


$ (document). Ready (function (e) {
$ ("#nian"). Change (function () {Changed event year changes reload days
Loadtian ();
})
$ ("#yue"). Change (function () {Month changed reload days
Loadtian ();
})
});

Introduction of external JS file

Display the selected year and month in the text box

Add a click event to the Confirmation button in the modal box
The value of the month and day is spelled into a string to find the text box to take a good value in the text box

Simple jquery Date Selection

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.