如果选择了jquery.ui.datepicker,则禁用日期

时间:2011-07-09 09:22:13

标签: javascript jquery jquery-ui

我的网站上有10个jquery.ui.datepicker。如果有人在第一个datepicker中选择今天的日期,想要在第二个datepicker中禁用今天的日期。所以喜欢在第3,第4和第6等等。意味着如果在任何日期选择器中选择了一个在其他日期选择器中无法选择的日期。

暂时我这样做了......但它从上到下工作......意味着datepicker 1到10 ....它不能从datepicker 10到1 ....意味着今天在datepicker10中选择,也可以选择相同的日期datepicker9 ...所以我也要禁用那个...按降序和升序禁用所选日期....

$(function () {
    $("#datepicker1, #datepicker2, #datepicker3, #datepicker4, #datepicker5, #datepicker6, #datepicker7, #datepicker8, #datepicker9, #datepicker10").datepicker({
        minDate: '+1D',
        maxDate: "+90D",
        showOn: "button",
        buttonImage: "images/calendar.gif",
        buttonImageOnly: true,
        dateFormat: "D, dd M, yy",
        onSelect: function (selectedDate) {
            if (this.id == 'datepicker1') {
                var dateMin = $('#datepicker1').datepicker("getDate");
                var rMin = new Date(dateMin.getFullYear(), dateMin.getMonth(), dateMin.getDate() + 1);
                var rMax = new Date(dateMin.getFullYear(), dateMin.getMonth(), dateMin.getDate() + 91);
                $('#datepicker2').datepicker("option", "minDate", rMin);
                $('#datepicker2').datepicker("option", "maxDate", rMax);
                $('#datepicker3').datepicker("option", "minDate", rMin);
                $('#datepicker3').datepicker("option", "maxDate", rMax);
                $('#datepicker4').datepicker("option", "minDate", rMin);
                $('#datepicker4').datepicker("option", "maxDate", rMax);
                $('#datepicker5').datepicker("option", "minDate", rMin);
                $('#datepicker5').datepicker("option", "maxDate", rMax);
                $('#datepicker6').datepicker("option", "minDate", rMin);
                $('#datepicker6').datepicker("option", "maxDate", rMax);
                $('#datepicker7').datepicker("option", "minDate", rMin);
                $('#datepicker7').datepicker("option", "maxDate", rMax);
                $('#datepicker8').datepicker("option", "minDate", rMin);
                $('#datepicker8').datepicker("option", "maxDate", rMax);
                $('#datepicker9').datepicker("option", "minDate", rMin);
                $('#datepicker9').datepicker("option", "maxDate", rMax);
                $('#datepicker10').datepicker("option", "minDate", rMin);
                $('#datepicker10').datepicker("option", "maxDate", rMax);
            }
            if (this.id == 'datepicker2') {
                var dateMin = $('#datepicker2').datepicker("getDate");
                var rMin = new Date(dateMin.getFullYear(), dateMin.getMonth(), dateMin.getDate() + 1);
                var rMax = new Date(dateMin.getFullYear(), dateMin.getMonth(), dateMin.getDate() + 91);
                $('#datepicker3').datepicker("option", "minDate", rMin);
                $('#datepicker3').datepicker("option", "maxDate", rMax);
                $('#datepicker4').datepicker("option", "minDate", rMin);
                $('#datepicker4').datepicker("option", "maxDate", rMax);
                $('#datepicker5').datepicker("option", "minDate", rMin);
                $('#datepicker5').datepicker("option", "maxDate", rMax);
                $('#datepicker6').datepicker("option", "minDate", rMin);
                $('#datepicker6').datepicker("option", "maxDate", rMax);
                $('#datepicker7').datepicker("option", "minDate", rMin);
                $('#datepicker7').datepicker("option", "maxDate", rMax);
                $('#datepicker8').datepicker("option", "minDate", rMin);
                $('#datepicker8').datepicker("option", "maxDate", rMax);
                $('#datepicker9').datepicker("option", "minDate", rMin);
                $('#datepicker9').datepicker("option", "maxDate", rMax);
                $('#datepicker10').datepicker("option", "minDate", rMin);
                $('#datepicker10').datepicker("option", "maxDate", rMax);
            }
            if (this.id == 'datepicker3') {
                var dateMin = $('#datepicker3').datepicker("getDate");
                var rMin = new Date(dateMin.getFullYear(), dateMin.getMonth(), dateMin.getDate() + 1);
                var rMax = new Date(dateMin.getFullYear(), dateMin.getMonth(), dateMin.getDate() + 91);
                $('#datepicker4').datepicker("option", "minDate", rMin);
                $('#datepicker4').datepicker("option", "maxDate", rMax);
                $('#datepicker5').datepicker("option", "minDate", rMin);
                $('#datepicker5').datepicker("option", "maxDate", rMax);
                $('#datepicker6').datepicker("option", "minDate", rMin);
                $('#datepicker6').datepicker("option", "maxDate", rMax);
                $('#datepicker7').datepicker("option", "minDate", rMin);
                $('#datepicker7').datepicker("option", "maxDate", rMax);
                $('#datepicker8').datepicker("option", "minDate", rMin);
                $('#datepicker8').datepicker("option", "maxDate", rMax);
                $('#datepicker9').datepicker("option", "minDate", rMin);
                $('#datepicker9').datepicker("option", "maxDate", rMax);
                $('#datepicker10').datepicker("option", "minDate", rMin);
                $('#datepicker10').datepicker("option", "maxDate", rMax);
            }
            if (this.id == 'datepicker4') {
                var dateMin = $('#datepicker4').datepicker("getDate");
                var rMin = new Date(dateMin.getFullYear(), dateMin.getMonth(), dateMin.getDate() + 1);
                var rMax = new Date(dateMin.getFullYear(), dateMin.getMonth(), dateMin.getDate() + 91);
                $('#datepicker5').datepicker("option", "minDate", rMin);
                $('#datepicker5').datepicker("option", "maxDate", rMax);
                $('#datepicker6').datepicker("option", "minDate", rMin);
                $('#datepicker6').datepicker("option", "maxDate", rMax);
                $('#datepicker7').datepicker("option", "minDate", rMin);
                $('#datepicker7').datepicker("option", "maxDate", rMax);
                $('#datepicker8').datepicker("option", "minDate", rMin);
                $('#datepicker8').datepicker("option", "maxDate", rMax);
                $('#datepicker9').datepicker("option", "minDate", rMin);
                $('#datepicker9').datepicker("option", "maxDate", rMax);
                $('#datepicker10').datepicker("option", "minDate", rMin);
                $('#datepicker10').datepicker("option", "maxDate", rMax);
            }
            if (this.id == 'datepicker5') {
                var dateMin = $('#datepicker5').datepicker("getDate");
                var rMin = new Date(dateMin.getFullYear(), dateMin.getMonth(), dateMin.getDate() + 1);
                var rMax = new Date(dateMin.getFullYear(), dateMin.getMonth(), dateMin.getDate() + 91);
                $('#datepicker6').datepicker("option", "minDate", rMin);
                $('#datepicker6').datepicker("option", "maxDate", rMax);
                $('#datepicker7').datepicker("option", "minDate", rMin);
                $('#datepicker7').datepicker("option", "maxDate", rMax);
                $('#datepicker8').datepicker("option", "minDate", rMin);
                $('#datepicker8').datepicker("option", "maxDate", rMax);
                $('#datepicker9').datepicker("option", "minDate", rMin);
                $('#datepicker9').datepicker("option", "maxDate", rMax);
                $('#datepicker10').datepicker("option", "minDate", rMin);
                $('#datepicker10').datepicker("option", "maxDate", rMax);
            }
            if (this.id == 'datepicker6') {
                var dateMin = $('#datepicker6').datepicker("getDate");
                var rMin = new Date(dateMin.getFullYear(), dateMin.getMonth(), dateMin.getDate() + 1);
                var rMax = new Date(dateMin.getFullYear(), dateMin.getMonth(), dateMin.getDate() + 91);
                $('#datepicker7').datepicker("option", "minDate", rMin);
                $('#datepicker7').datepicker("option", "maxDate", rMax);
                $('#datepicker8').datepicker("option", "minDate", rMin);
                $('#datepicker8').datepicker("option", "maxDate", rMax);
                $('#datepicker9').datepicker("option", "minDate", rMin);
                $('#datepicker9').datepicker("option", "maxDate", rMax);
                $('#datepicker10').datepicker("option", "minDate", rMin);
                $('#datepicker10').datepicker("option", "maxDate", rMax);
            }
            if (this.id == 'datepicker7') {
                var dateMin = $('#datepicker7').datepicker("getDate");
                var rMin = new Date(dateMin.getFullYear(), dateMin.getMonth(), dateMin.getDate() + 1);
                var rMax = new Date(dateMin.getFullYear(), dateMin.getMonth(), dateMin.getDate() + 91);
                $('#datepicker8').datepicker("option", "minDate", rMin);
                $('#datepicker8').datepicker("option", "maxDate", rMax);
                $('#datepicker9').datepicker("option", "minDate", rMin);
                $('#datepicker9').datepicker("option", "maxDate", rMax);
                $('#datepicker10').datepicker("option", "minDate", rMin);
                $('#datepicker10').datepicker("option", "maxDate", rMax);
            }
            if (this.id == 'datepicker8') {
                var dateMin = $('#datepicker8').datepicker("getDate");
                var rMin = new Date(dateMin.getFullYear(), dateMin.getMonth(), dateMin.getDate() + 1);
                var rMax = new Date(dateMin.getFullYear(), dateMin.getMonth(), dateMin.getDate() + 91);
                $('#datepicker9').datepicker("option", "minDate", rMin);
                $('#datepicker9').datepicker("option", "maxDate", rMax);
                $('#datepicker10').datepicker("option", "minDate", rMin);
                $('#datepicker10').datepicker("option", "maxDate", rMax);
            }
            if (this.id == 'datepicker9') {
                var dateMin = $('#datepicker9').datepicker("getDate");
                var rMin = new Date(dateMin.getFullYear(), dateMin.getMonth(), dateMin.getDate() + 1);
                var rMax = new Date(dateMin.getFullYear(), dateMin.getMonth(), dateMin.getDate() + 91);
                $('#datepicker10').datepicker("option", "minDate", rMin);
                $('#datepicker10').datepicker("option", "maxDate", rMax);
            }
        }
    });
});

1 个答案:

答案 0 :(得分:0)

未经测试但您应该可以

.select(function() {
  var dateMin = $(this)("getDate");
  var rMin = new Date(dateMin.getFullYear(), dateMin.getMonth(), dateMin.getDate() + 1);
  var rMax = new Date(dateMin.getFullYear(), dateMin.getMonth(), dateMin.getDate() + 91);
  $('.datepicker').not(this).each(function() {
    $(this).datepicker("option", "minDate", rMin);
    $(this).datepicker("option", "maxDate", rMax);
  });    
 });