PHP method to realize three-level linkage of year, month and day

墨辰丷
Release: 2023-03-27 11:26:02
Original
1385 people have browsed it

This article mainly introduces the relevant information of the three-level linkage example code of PHP year, month and day. Friends who need it can refer to

Three-level linkage of year, month and day

<input type="text" id="hs"> <input type="button" id="btn" value="提交">
<span id="rent"></span>
Copy after login

The three-level linkage of this year, month and day mainly uses the select tag

var str = "<select id=&#39;year&#39;></select>  <select id=&#39;month&#39;>" +
    "</select>  <select id=&#39;day&#39;></select>";
  $("#rent").html(str);

  fullyear();
  fullmonth();
  fullday();

  //当其中一个改变,后面的要跟着改变

  $("#year").change(function(){

    fullday();
  });
  $("#month").change(function(){

    fullday();
  });


  function fullyear()

  {
    var d = new Date();
    var year = d.getFullYear();
    str ="";
    for(var i=year-5;i<year+6;i++)
    {
      if(i==year)
      {
        str += "<option selected=&#39;selected&#39; value=&#39;"+i+"&#39;>"+i+"</option>";
      }
      else {
        str +="<option value=&#39;"+i+"&#39;>"+i+"</option>"
      }
    }
    $("#year").html(str);
  }

  function fullmonth()
  {
    var d = new Date();
    var month = d.getMonth()+1;
    str ="";
    for(var j=1;j<13;j++)
    {
      if(j==month)
      {
        str += "<option selected=&#39;selected&#39; value=&#39;"+j+"&#39;>"+j+"</option>";
      }
      else {
        str +="<option value=&#39;"+j+"&#39;>"+j+"</option>"
      }
    }
    $("#month").html(str);

  }

  function fullday()
  {
    var d = new Date();
    var day = d.getDate();
    var year=$("#year").val();
    var month=$("#month").val();
    var rq=31;
    str ="";
    if(month==4|| month==6|| month==9|| month===11)
    {
      rq=30;
    }
    else if(month==2)
    {
      if(year%4==0 && year%100!=0 || year%400==0)
      {
        rq=29;//闰年
      }
      else{
        rq=28; //不是闰年
      }
    }
    for(var n=1;n<rq+1;n++)
    {
      if(n==day)
      {
        str +="<option selected=&#39;selected&#39; value=&#39;"+n+"&#39;>"+n+"</option>";
      }
      else
      {
        str +="<option value=&#39;"+n+"&#39;>"+n+"</option>";
      }
    }

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

  } //到这里就完成了下拉列表的内容了,下一步要做的是把内容存到表单中
Copy after login

$("#btn").click(function(){
 var nian=$("#year").val();
 var yue=$("#month").val();
 var ri=$("#day").val();

var time=nian+"-"+yue+"-"+ri+"";

 $("#hs").val(time)


})
Copy after login

Related recommendations:

JS simple method to get the current year, month, day, week

jQuery gets the age implementation code based on the birthyear, month, and day

js formats the current time into year, month, and day Detailed explanation of hour, minute and second format

The above is the detailed content of PHP method to realize three-level linkage of year, month and day. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template