Self-written date, year, month, day, three levels of linkage effect jquery plug-in,

Source: Internet
Author: User
Tags jquery library

Self-written date, year, month, day, three levels of linkage effect jquery plug-in,


Haha, I haven't written any new blog for a long time, because I have been studying the function of using jquery UI to intercept pictures recently. Haha


Back to the truth, sometimes we will use the three-level linkage effect of one year, month, and date on the user data page. I found it on the Internet and didn't see how suitable IT IS (mainly because the silk is inclined to use jquery as a plug-in, haha ~~)

This blog post aims to show you how to use jquery to write plug-ins.

The principle is very simple, that is, to determine the leap year, and then re-append option to the select that displays the date when the select change event

(PS. I don't know why. js has the appendchild method, which can be inserted continuously without overwriting, but jquery does not. The new append child will overwrite the previous one, so it feels so painful to say, this diaosi intends to write a comprehensive plug-in the future to encapsulate these useful small functions. Please wait for a while ~~)

Render manager is unlimited, and chrysanthemums are everywhere ~~~ The following code is displayed. I named him birthday.


; (Function ($) {$. fn. birthday = function (options) {var opts = $. extend ({}, $. fn. birthday. defaults, options); // The integration parameter var $ year = $ (this ). children ("select [name =" + opts. year + "]"); var $ month = $ (this ). children ("select [name =" + opts. month + "]"); var $ day = $ (this ). children ("select [name =" + opts. day + "]"); MonHead = [, 30,]; return this. each (function () {var y = new Date (). getFullYea R (); var con = ""; // Add year for (I = y; I >=( y-80); I --) {con + = "<option value = '" + I + "'>" + I + "year" + "</option>" ;}$ year. append (con); con = ""; // Add the month for (I = 1; I <= 12; I ++) {con + = "<option value = '" + I + "'>" + I + "month" + "</option>" ;}$ month. append (con); con = ""; // Add date var n = MonHead [0]; // The default value is January for (I = 1; I <= n; I ++) {con + = "<option value = '" + I + "'>" + I + "day" + "</option>" ;}$ day. append (con); $. fn. birthday. chan Ge ($ (this) ;};}; $. fn. birthday. change = function (obj) {obj. children ("select [name =" + $. fn. birthday. defaults. year + "], select [name =" + $. fn. birthday. defaults. month + "]"). change (function () {var $ year = obj. children ("select [name =" + $. fn. birthday. defaults. year + "]"); var $ month = obj. children ("select [name =" + $. fn. birthday. defaults. month + "]"); var $ day = obj. children ("select [name =" + $. fn. birthday. defau Lts. day + "]"); $ day. empty (); var selectedYear = $ year. find ("option: selected "). val (); var selectedMonth = $ month. find ("option: selected "). val (); if (selectedMonth = 2 & $. fn. birthday. isRunYear (selectedYear) {// if it is a leap year var c = ""; for (var I = 1; I <= 29; I ++) {c + = "<option value = '" + I + "'>" + I + "+" </option> ";}$ day. append (c);} else {// if not a leap year also did not select February var c = ""; for (var I = 1; I <= MonHead [selectedMonth-1]; I ++) {C + = "<option value = '" + I + "'>" + I + "day" + "</option>" ;}$ day. append (c) ;}}) ;}; $. fn. birthday. isRunYear = function (selectedYear) {return (0 = selectedYear % 4 & (selectedYear % 100! = 0 | selectedYear % 400 = 0) ;};$. fn. birthday. defaults = {year: "year", month: "month", day: "day" };}) (jQuery );

The code is short but easy to use. Its usage is as follows:

In the html section, you need to set the name for the select statement.

<div id="birthday_container"><select name="year"></select><select name="month"></select><select name="day"></select></div>

Js section. After this js file is introduced (note that it is placed behind the referenced jquery library file), introduce the following code after the page is loaded:

// Three-level date Association $ ("# birthday_container"). birthday ();

Isn't it easy? haha, provide the js:

Jquery third-level linkage plug-in for date, year, month, and day

No code, no advertisement ~


Add a poem for entertainment ~~ Guess the artistic conception, haha.

After the rain,

Shepherd's scalpers,

Under juju dongli,

Bow down to my hometown.


If you have any questions or suggestions, add QQ: 1740437






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.