zoukankan      html  css  js  c++  java
  • 日期,为下拉列表添加日期,优化,目前本人博客上最优的解决方案,之前学习的通过判断得到平年闰年,而这个是让系统自动去判断,无须if判断,代码示例

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <%
    request.setCharacterEncoding("utf-8");
    response.setCharacterEncoding("utf-8");
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'date.jsp' starting page</title>
        
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">    
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
        <script type="text/javascript" src="jquery/jquery-1.8.0.js"></script>
        <script type="text/javascript">
            //获取年份
            function getYear(){
                //获取下拉列表
                var year=$("#year");
                //实例化日期对象
                var date=new Date();
                //循环添加
                for(var i=date.getFullYear();i>=1990;i--){
                    //$("<option>")就是<option></option>
                    //val(i).text(i)就是<option value='+i+'>i</option>
                    //最后year.append追加下拉选项
                    year.append($("<option>").val(i).text(i));
                }
            }
            
            //获取月份
            function getMonth(){
                //月份下拉列表
                var month=$("#month");
                for(var i=1;i<=12;i++){
                    month.append($("<option>").val(i).text(i));
                }
            }
            
            //获取天数
            function getDate(){
                var yearVal=$("#year").val();
                var monthVal=$("#month").val();
                var day=$("#date");
                //清空
                day.empty();
                //设置日期函数,0目的是为了得到当前月份的最大天数
                //如果date设置0的话,得到是上一个月的最后一天,平年和闰年会自动去判断
                //如果date设置为1的话,得到的是这月的第一天
                //原因是monthVal得到的是准确的月份,而通过date获取的月份得到是0-11
                //这样的话,new Date(yearVal, monthVal,0);设置的就是前一个月的最后日期
                var date=new Date(yearVal, monthVal,0);
                for(var i=1;i<=date.getDate();i++){
                    day.append($("<option>").val(i).text(i));
                }
            }
            
            //为下拉列表添加值
            function showDate(){
                getYear();
                getMonth();
                getDate();
            }
        </script>
      </head>
     
      <body onload="showDate()">
            年份:<select id="year" onchange="getDate()"></select>
            月份:<select id="month" onchange="getDate()"></select>
            天:<select id="date" onchange="getDate()"></select>
      </body>
    </html>

  • 相关阅读:
    饿了么ElementUI table遇到的问题
    Window命令行杀进程
    网络监控流量工具
    记一次Linux系统被入侵的过程
    sftp ftp文件同步方案
    清除oracle归档日志
    TCP连接复用
    Sftp搭建与配置参考
    setfacl命令
    tips
  • 原文地址:https://www.cnblogs.com/danmao/p/3871258.html
Copyright © 2011-2022 走看看