Realize three-level linkage of year, month and day based on JavaScript

Realize three-level linkage of year, month and day based on JavaScript

This article shares the specific code for JavaScript to achieve three-level linkage of year, month and day for your reference. The specific content is as follows

Code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Year, month, day three-level linkage</title>
</head>
<body onload="initYear(),initMonth()">
<select id="year"></select>year<select id="month" onchange="initDate()"></select>month<select id="date"></select>day<script>
    /**
     * Initialize year */
    function initYear() {
        //Get the current year let curYear = new Date().getFullYear();
        //Get the year list object let yearObj = document.getElementById("year");
        yearObj.options.add(new Option("---Please select year---", ""));
        for (let year = curYear; year > curYear - 100; year--) {
            let option = new Option(year, year);
            yearObj.options.add(option);
        }
    }

    /**
     * Initialize month */
    function initMonth() {
        //Get the year list object let monthObj = document.getElementById("month");
        monthObj.options.add(new Option("---Please select a month---", ""));
        for (let month = 1; month <= 12; month++) {
            let option = new Option(month, month);
            monthObj.options.add(option);
        }
    }

    /**
     * Initialization date */
    function initDate() {
        let dateObj = document.getElementById("date");
        //Get the selected month of the month let month = document.getElementById("month").value;
        //When the month is selected, the corresponding date will pop up dateObj.options.add(new Option("---Please select a date---", ""));
        //Convert month into a number month = parseInt(month);
        //Define the number of days per month let days = 31;
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                days = 30;
                break;
            case 2:
                //Need to determine whether it is a leap year and get the currently selected year let year = document.getElementById("year").value;
                if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
                    days = 29;
                } else {
                    days = 28;
                }
                break;
        }
        // Loop through the days obtained for (let i = 1; i <= days; i++) {
            let option = new Option(i, i);
            dateObj.options.add(option);
        }
    }
</script>
</body>
</html>

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • js realizes the three-level linkage of year, month and day forms

<<:  Use and analysis of Mysql Explain command

>>:  Security considerations for Windows server management

Recommend

HTML Tutorial: Collection of commonly used HTML tags (4)

Related articles: Beginners learn some HTML tags ...

Detailed explanation of Vue3 life cycle functions and methods

1. Overview The so-called life cycle function is ...

Application scenarios and design methods of MySQL table and database sharding

Many friends have asked in forums and message are...

Detailed explanation of react setState

Table of contents Is setState synchronous or asyn...

Detailed explanation of creating stored procedures and functions in mysql

Table of contents 1. Stored Procedure 1.1. Basic ...

Example analysis of mysql shared lock and exclusive lock usage

This article uses examples to illustrate the usag...

A brief discussion on MySql views, triggers and stored procedures

view What is a view? What is the role of a view? ...

Detailed explanation of MySQL syntax, special symbols and regular expressions

Mysql commonly used display commands 1. Display t...

How to insert a link in html

Each web page has an address, identified by a URL...

Detailed analysis of javascript data proxy and events

Table of contents Data Brokers and Events Review ...

Detailed explanation of JavaScript clipboard usage

(1) Introduction: clipboard.js is a lightweight J...

Detailed explanation of the principle and usage of MySQL stored procedures

This article uses examples to explain the princip...

Detailed introduction and usage examples of map tag parameters

Map tags must appear in pairs, i.e. <map> .....