Detailed description of jquery plug-in datepicker

Source: Internet
Author: User
Jquery is a rare and excellent Javascript script development library, and many plug-ins based on it are also very standard and excellent. If you miss this beautiful scenery, it would be a pity, for example, datepicker.
Generally, the front-end of the MIS system, especially the user registration page, will have a date input box such as "Year of birth, the simplest way is to use a <input type = "text"/> label, which has many drawbacks: the first is the matching with the database field type, and the second is the validity of the input date, such as "13 months" or a leap year. If you go deeper, there are still many issues worth further consideration. Currently, the most popular method is to use the drop-down menu <SELECT> <option> </SELECT> to construct a structure. However, this method is not satisfactory regardless of interactivity, complexity, and portability, because at least three linked drop-down menus need to be built, you need to write a large number of scripts to process date legitimacy.
Datepicker brings beautiful spring. Let's see what it looks like when using the default style:


datepicker is favored by many developers for its completely Gui-like user experience, dazzling Dynamic Display Effect, precise date control, and highly flexible parameter configuration, this includes the well-known Google, which uses this script in its Google Calendar project. If you are interested, you can check it out. By the way, the default effect is that in Javascript, users only need to write one sentence to achieve it. How can this problem be solved? Follow me:
1. download the jquery core file. datepicker is a lightweight plug-in. You only need to download the jquery min version, and then download datepicker (containing jquery1.2.6 _ min ), you can also download it from the official website: http://marcgrabanski.com/pages/code/jquery-ui-datepicker.
2. reference the two downloaded JS files in HTML:

    1. <Script Language="JavaScript" SRC=JS/jquery-1.2.6.min.js"></Script>
    2. <Script Language="JavaScript" SRC="JS/UI. datepicker. js"></Script>

3. introduce the default style sheet file in HTML. This file is also in the compressed package. If you download the file from the official website, the CSS file will be downloaded on the homepage. You can also select CSS for other skins:

    1. <Link REL="Stylesheet" Href="JS/ui.datepicker.css" Type="Text/CSS" Media="Screen" Title="Core CSS file" Charset="UTF-8" />

4. Insert text fields in HTML. It is best to set them to read-only and do not accept manual input from users. This prevents format confusion and marks them with IDs.

    1. <Input ID="Dateinput" Type="Text" Readonly="Readonly"/>

5. Compile JSCodeTo achieve the final effect.

    1. <Script language ="JavaScript">
    2. $ (Document). Ready (Function(){
    3. $ ('# Dateinput'). Datepicker ();
    4. });
    5. </SCRIPT>

In this way, a text field of Date input is basically completed, but it is in English. According to different MIS systems, some target users are grade users. We recommend that you change the interface to Chinese, you can perform this operation by slightly modifying the function just now, like this:

  1. <Script language ="JavaScript">
  2. $ (Document). Ready (function (){
  3. $ ('# Dateinput'). Datepicker ({
  4. Dateformat:'Yy-mm-dd',// Date Format, set by yourself
  5. Buttonimage:'Calendar.gif',// Set the image path of the button
  6. Buttonimageonly:True,// Show an image trigger without any button.
  7. Showon:'Both',// Trigger condition. Both indicates that clicking the text field and image button take effect.
  8. Yearrange:'2017: 66661',// Year range
  9. Cleartext:'Clear',// Do not write comments in detail below.
  10. Closetext:'Close',
  11. Prevtext:'October 11',
  12. Nexttext:'August 11',
  13. Currenttext:'',
  14. Monthnames :['August 11','August 11','August 11','August 11','August 11','August 11','August 11','August 11','August 11','August 11','August 11','August 11'],
  15. });
  16. });
  17. </SCRIPT>

OK. As a result, I wrote the following page code as per my requirements. It is for your reference only. Please try it yourself:

  1. <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <Html Xmlns=Http://www.w3.org/1999/xhtml">
  3. <Head>
  4. <Meta HTTP-equiv="Content-Type" Content="Text/html; charset = UTF-8" />
  5. <Title>Untitled document</Title>
  6. link rel = "stylesheet" href = "JS/ui.datepicker.css" type = " text/CSS " media = " screen" title = "core CSS file" charset = "UTF-8" />
  7. <Script Language="JavaScript" SRC=JS/jquery-1.2.6.min.js"></Script>
  8. <Script Language="JavaScript" SRC="JS/UI. datepicker. js"></Script>
  9. <Script Language="JavaScript">
  10. $ (Document). Ready (function (){
  11. VaRYearfrom=NewDate (). getyear ()-60 + 1900;
  12. VaRYearto=NewDate (). getyear ()-18 + 1900;
  13. $ ('# Dateinput'). datepicker ({
  14. Dateformat: 'yy-mm-dd ',
  15. Buttonimage: 'calendar.gif ',
  16. Buttonimageonly: True,
  17. Showon: 'both ',
  18. Yearrange: yearfrom + ':' + yearto,
  19. Cleartext: 'clear ',
  20. Closetext: 'close ',
  21. Prevtext: 'August 11 ',
  22. Nexttext: 'August 11 ',
  23. Currenttext :'',
  24. Monthnames ', 'August 11', 'August 11', 'August 11'],
  25. });
  26. });
  27. </Script>
  28. </Head>
  29. <Body>
  30. <Input ID="Dateinput" Type="Text" Readonly="Readonly"/>
  31. </Body>
  32. </Html>

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.