前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >jQuery年月日(年龄)选择插件

jQuery年月日(年龄)选择插件

作者头像
王小婷
发布2019-04-09 17:39:32
2.4K0
发布2019-04-09 17:39:32
举报
文章被收录于专栏:编程微刊

下载地址:https://www.helloweba.net/javascript/263.html


代码语言:javascript
复制
<!DOCTYPE HTML>
<html>

    <head>
        <meta charset="utf-8">
        <title>jQuery年月日(生日)选择器</title>
        <style>

        </style>
        <script type="text/javascript" src="jquery.js"></script>
    </head>

    <body>
        <div class="demo">
            <p>
                默认值:<br/>
                <label>生日:</label>
                <select class="sel_year" rel="2000"></select>年
                <select class="sel_month" rel="2"></select>月
                <select class="sel_day" rel="14"></select>日
            </p>
        </div>
        <script type="text/javascript" src="birthday.js"></script>
        <script>
            $(function() {
                $.ms_DatePicker({
                    YearSelector: ".sel_year",
                    MonthSelector: ".sel_month",
                    DaySelector: ".sel_day"
                });
                $.ms_DatePicker();
            });
        </script>
    </body>

</html>

birthday.js内容:(来自github)

代码语言:javascript
复制
(function($){
$.extend({
ms_DatePicker: function (options) {
            var defaults = {
                YearSelector: "#sel_year",
                MonthSelector: "#sel_month",
                DaySelector: "#sel_day",
                FirstText: "--",
                FirstValue: 0
            };
            var opts = $.extend({}, defaults, options);
            var $YearSelector = $(opts.YearSelector);
            var $MonthSelector = $(opts.MonthSelector);
            var $DaySelector = $(opts.DaySelector);
            var FirstText = opts.FirstText;
            var FirstValue = opts.FirstValue;

            // 初始化
            var str = "<option value=\"" + FirstValue + "\">" + FirstText + "</option>";
            $YearSelector.html(str);
            $MonthSelector.html(str);
            $DaySelector.html(str);

            // 年份列表
            var yearNow = new Date().getFullYear();
            var yearSel = $YearSelector.attr("rel");
            for (var i = yearNow; i >= 1900; i--) {
                var sed = yearSel==i?"selected":"";
                var yearStr = "<option value=\"" + i + "\" " + sed+">" + i + "</option>";
                $YearSelector.append(yearStr);
            }

            // 月份列表
            var monthSel = $MonthSelector.attr("rel");
            for (var i = 1; i <= 12; i++) {
                var sed = monthSel==i?"selected":"";
                var monthStr = "<option value=\"" + i + "\" "+sed+">" + i + "</option>";
                $MonthSelector.append(monthStr);
            }

            // 日列表(仅当选择了年月)
            function BuildDay() {
                if ($YearSelector.val() == 0 || $MonthSelector.val() == 0) {
                    // 未选择年份或者月份
                    $DaySelector.html(str);
                } else {
                    $DaySelector.html(str);
                    var year = parseInt($YearSelector.val());
                    var month = parseInt($MonthSelector.val());
                    var dayCount = 0;
                    switch (month) {
                        case 1:
                        case 3:
                        case 5:
                        case 7:
                        case 8:
                        case 10:
                        case 12:
                            dayCount = 31;
                            break;
                        case 4:
                        case 6:
                        case 9:
                        case 11:
                            dayCount = 30;
                            break;
                        case 2:
                            dayCount = 28;
                            if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) {
                                dayCount = 29;
                            }
                            break;
                        default:
                            break;
                    }
                    
                    var daySel = $DaySelector.attr("rel");
                    for (var i = 1; i <= dayCount; i++) {
                        var sed = daySel==i?"selected":"";
                        var dayStr = "<option value=\"" + i + "\" "+sed+">" + i + "</option>";
                        $DaySelector.append(dayStr);
                    }
                }
            }
            $MonthSelector.change(function () {
                BuildDay();
            });
            $YearSelector.change(function () {
                BuildDay();
            });
            if($DaySelector.attr("rel")!=""){
                BuildDay();
            }
        } // End ms_DatePicker
});
})(jQuery);
怎么得到选择的值?
代码语言:javascript
复制
function getBirthday() {
                var date = "";
                date += $(".sel_year").val();
                date += "-";
                if (parseInt($(".sel_month").val()) < 10) {
                    date += "0";
                    date += $(".sel_month").val();
                } else {
                    date += $(".sel_month").val();
                }
                date += "-";
                if (parseInt($(".sel_day").val()) < 10) {
                    date += "0";
                    date += $(".sel_day").val();
                } else {
                    date += $(".sel_day").val();
                }
                return date;
            }

原文作者:祈澈姑娘 技术博客:https://www.jianshu.com/u/05f416aefbe1

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019.04.01 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 怎么得到选择的值?
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档