前端实现只显示年月日

举报
tea_year 发表于 2021/12/30 00:54:21 2021/12/30
【摘要】 视频课:https://edu.csdn.net/course/play/7621 只显示年月日的前端代码:jquery+js <!DOCTYPE HTML><html><head><meta charset="utf-8"><title>演示:jQuery年月日(生日)选择器...

视频课:https://edu.csdn.net/course/play/7621

只显示年月日的前端代码:jquery+js


  
  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>演示:jQuery年月日(生日)选择器</title>
  6. <link rel="stylesheet" type="text/css" href="../css/main.css" />
  7. <style>
  8. .demo{width:420px; margin:40px auto 0 auto; min-height:250px;}
  9. .demo p{line-height:42px; font-size:16px}
  10. </style>
  11. <script type="text/javascript" src="jquery.js"></script>
  12. </head>
  13. <body>
  14. <div id="header">
  15. <div id="logo"><h1><a href="http://www.helloweba.com" title="返回helloweba首页">helloweba</a></h1></div>
  16. <div class="demo_topad"><script src="/js/ad_js/demo_topad.js" type="text/javascript"></script></div>
  17. </div>
  18. <div id="main">
  19. <h2 class="top_title"><a href="http://www.helloweba.com/view-blog-263.html">jQuery年月日(生日)选择器</a></h2>
  20. <div class="demo">
  21. <p>
  22. 初始化:<br/>
  23. <label>生日:</label>
  24. <select id="sel_year"></select>
  25. <select id="sel_month"></select>
  26. <select id="sel_day"></select>
  27. </p>
  28. <p>
  29. 默认值:<br/>
  30. <label>生日:</label>
  31. <select class="sel_year" rel="2000"></select>
  32. <select class="sel_month" rel="2"></select>
  33. <select class="sel_day" rel="14"></select>
  34. </p>
  35. </div>
  36. <br/><div class="ad_76090"><script src="/js/ad_js/bd_76090.js" type="text/javascript"></script></div><br/>
  37. </div>
  38. <script type="text/javascript" src="birthday.js"></script>
  39. <script>
  40. $(function () {
  41. $.ms_DatePicker({
  42. YearSelector: ".sel_year",
  43. MonthSelector: ".sel_month",
  44. DaySelector: ".sel_day"
  45. });
  46. $.ms_DatePicker();
  47. });
  48. </script>
  49. <div id="footer">
  50. <p>Powered by helloweba.com 允许转载、修改和使用本站的DEMO,但请注明出处:<a href="http://www.helloweba.com">www.helloweba.com</a></p>
  51. </div>
  52. <p id="stat"><script type="text/javascript" src="/js/tongji.js"></script></p>
  53. </body>
  54. </html>

所需要的js代码,用来计算年月日,包含闰年之类的


  
  1. (function($){
  2. $.extend({
  3. ms_DatePicker: function (options) {
  4. var defaults = {
  5. YearSelector: "#sel_year",
  6. MonthSelector: "#sel_month",
  7. DaySelector: "#sel_day",
  8. FirstText: "--",
  9. FirstValue: 0
  10. };
  11. var opts = $.extend({}, defaults, options);
  12. var $YearSelector = $(opts.YearSelector);
  13. var $MonthSelector = $(opts.MonthSelector);
  14. var $DaySelector = $(opts.DaySelector);
  15. var FirstText = opts.FirstText;
  16. var FirstValue = opts.FirstValue;
  17. // 初始化
  18. var str = "<option value=\"" + FirstValue + "\">" + FirstText + "</option>";
  19. $YearSelector.html(str);
  20. $MonthSelector.html(str);
  21. $DaySelector.html(str);
  22. // 年份列表
  23. var yearNow = new Date().getFullYear();
  24. var yearSel = $YearSelector.attr("rel");
  25. for (var i = yearNow; i >= 1900; i--) {
  26. var sed = yearSel==i?"selected":"";
  27. var yearStr = "<option value=\"" + i + "\" " + sed+">" + i + "</option>";
  28. $YearSelector.append(yearStr);
  29. }
  30. // 月份列表
  31. var monthSel = $MonthSelector.attr("rel");
  32. for (var i = 1; i <= 12; i++) {
  33. var sed = monthSel==i?"selected":"";
  34. var monthStr = "<option value=\"" + i + "\" "+sed+">" + i + "</option>";
  35. $MonthSelector.append(monthStr);
  36. }
  37. // 日列表(仅当选择了年月)
  38. function BuildDay() {
  39. if ($YearSelector.val() == 0 || $MonthSelector.val() == 0) {
  40. // 未选择年份或者月份
  41. $DaySelector.html(str);
  42. } else {
  43. $DaySelector.html(str);
  44. var year = parseInt($YearSelector.val());
  45. var month = parseInt($MonthSelector.val());
  46. var dayCount = 0;
  47. switch (month) {
  48. case 1:
  49. case 3:
  50. case 5:
  51. case 7:
  52. case 8:
  53. case 10:
  54. case 12:
  55. dayCount = 31;
  56. break;
  57. case 4:
  58. case 6:
  59. case 9:
  60. case 11:
  61. dayCount = 30;
  62. break;
  63. case 2:
  64. dayCount = 28;
  65. if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) {
  66. dayCount = 29;
  67. }
  68. break;
  69. default:
  70. break;
  71. }
  72. var daySel = $DaySelector.attr("rel");
  73. for (var i = 1; i <= dayCount; i++) {
  74. var sed = daySel==i?"selected":"";
  75. var dayStr = "<option value=\"" + i + "\" "+sed+">" + i + "</option>";
  76. $DaySelector.append(dayStr);
  77. }
  78. }
  79. }
  80. $MonthSelector.change(function () {
  81. BuildDay();
  82. });
  83. $YearSelector.change(function () {
  84. BuildDay();
  85. });
  86. if($DaySelector.attr("rel")!=""){
  87. BuildDay();
  88. }
  89. } // End ms_DatePicker
  90. });
  91. })(jQuery);

如果要测试取出的年月日


  
  1. <script>
  2. function asss(){
  3. alert($("#sel_year").val());
  4. alert($("#sel_month").val());
  5. alert($("#sel_day").val());
  6. }
  7. </script>


文章来源: aaaedu.blog.csdn.net,作者:tea_year,版权归原作者所有,如需转载,请联系作者。

原文链接:aaaedu.blog.csdn.net/article/details/75943053

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。