简体   繁体   English

jQuery UI Datepicker 单独的日期/月+年

[英]jQuery UI Datepicker separate date/month+year

I want a datepicker like the one on booking.com.我想要一个日期选择器,就像booking.com 上的那个一样。

It automatically changes the day+date of the month when a month+year in another select box is changed.当另一个选择框中的月份+年份更改时,它会自动更改月份的日期+日期。 I have had a look at the API of jQuery datepicker but I couldn't find anything that would help me.我查看了 jQuery datepicker 的 API,但找不到任何可以帮助我的东西。

See i Created One Fiddle Where you Select Date From DatePicker then it will change in Day/Month/year on Another Field .请参阅我创建了一个小提琴,您从 DatePicker 中选择日期,然后它将在另一个字段的日/月/年中更改。 : Demo :演示

$(function() {
$("#fullDate").datepicker({
    onClose: function(dateText, inst) {
        $('#year').val(dateText.split('/')[2]);
        $('#month').val(dateText.split('/')[0]);
        $('#day').val(dateText.split('/')[1]);
    }
});
});

Thanks for your input.感谢您的输入。 I finally figured how to get it working.我终于想出了如何让它工作。

So here is the answer for anyone else who wants this kind of functionality.所以这里是任何想要这种功能的人的答案。

HTML HTML

<select name="day" id="day">
     <option value="">Select Day</option>
</select>
<select name="month" id="month">
     <option value="">Select Month</option>
</select>

JS JS

function getMonths() {
    var months = new Array(12);
    months[0] = "Jan";
    months[1] = "Feb";
    months[2] = "Mar";
    months[3] = "Apr";
    months[4] = "May";
    months[5] = "Jun";
    months[6] = "Jul";
    months[7] = "Aug";
    months[8] = "Sep";
    months[9] = "Oct";
    months[10] = "Nov";
    months[11] = "Dec";
    var d = new Date();
    var month = d.getMonth();
    var year = d.getFullYear();
    var monthsArray = new Array(13);
    for (var i = 0; i < 13; i++) {
        var now = new Date();
        if (now.getMonth() == 11) {
            var current = new Date(now.getFullYear() + 1, 0, 1);
        } else {
            var current = new Date(now.getFullYear(), now.getMonth() + i, 1);
        }
        monthsArray[current.getMonth() + 1 + '_' + current.getFullYear()] = months[current.getMonth()] + ' ' + current.getFullYear();
    }
    return monthsArray;
}

function getDaysInMonth(month, year, daySelector) {
    var names = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
    var date = new Date(year, month - 1, 1);
    var days = [];
    while (date.getMonth() == month - 1) {
        days[date.getDate()] = (date.getDate() + " " + names[date.getDay()]);
        date.setDate(date.getDate() + 1);
    }
    daySelector.find('option').remove();
    daySelector.append($("<option></option>").attr("value", '').text("Day"));
    for (var key in days) {
        daySelector.append($("<option></option>").attr("value", key).text(days[key]));
    }
}

var months = getMonths();
for (var key in months) {
    $('#month')
        .append($("<option></option>")
        .attr("value", key)
        .text(months[key]));
}

$('#month').change(function () {

    var monthYear = $('#month').val().split('_');
    getDaysInMonth(monthYear[0], monthYear[1], $('#day'));

});

Demo & Code演示和代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM