Several related jQuery UI datepickers

I want to be able to bind multiple pairs of jQuery UI datepicker date instances so that the second of each pair cannot select a date earlier than the first. I am starting this example .

Example:

<ul>
<li>
<input class="counter" name="counter" type="hidden" value="43"/>
<label>Start: </label><input name="start_43" id="start_43" size="10" />
<label>End: </label><input name="end_43" id="end_43" size="10" />
</li>

<li>
<input class="counter" name="counter" type="hidden" value="44"/>
<label>Start: </label><input name="start_44" id="start_44" size="10" />
<label>End: </label><input name="end_44" id="end_44" size="10" />
</li>
</ul>

I iterate over instances by clicking the counter number:

$(document).ready(function() {
    var starts = $("input[name='counter']");
    var dates = new Array();
    starts.each(function(){
        var x = this.value;
        // http://jqueryui.com/demos/datepicker/#date-range
        dates[x] = $( "#start_"+x+", #end_"+x ).datepicker({
            onSelect: function( selectedDate ) {
            var option = this.id == "#start_"+x ? "minDate" : "maxDate",
            instance = $( this ).data( "datepicker" );
            date = $.datepicker.parseDate(
            instance.settings.dateFormat ||
            $.datepicker._defaults.dateFormat,
            selectedDate, instance.settings );
            dates[x].not( this ).datepicker( "option", option, date );
            }
        });
    });
});

This works to include a datepicker on each input, but that does not stop the second instance from picking the date before the first. This actually requires the second instance to select a date before the first. When selected, the second instance fills both inputs!

Can anyone see where I'm wrong?

+5
source share
2 answers

100% , parseDate, . , , - this.id. ID # DOM.

onSelect: function(selectedDate) {
    var option = this.id.indexOf("start_") != -1 ? "minDate" : "maxDate";
    dates[i].not(this).datepicker("option", option, selectedDate);
}

, ( , , ).:]

+4

$("#FirstCal").datepicker({
        dateFormat: 'M d, yy',
        navigationAsDateFormat: true, prevText: 'M', nextText: 'M',
        changeMonth: true,
        changeYear: true,
        showOn: "both",
        showStatus: true,
        firstDay: 0,
        changeFirstDay: false,
        beforeShow: customRange,
        buttonImage: '/Content/images/Calendar_img.png',
        buttonImageOnly: true
    });

    $("#secondCal").datepicker({
        dateFormat: 'M d, yy',
        navigationAsDateFormat: true, prevText: 'M', nextText: 'M',
        changeMonth: true,
        changeYear: true,
        showOn: "both",
        showStatus: true,
        firstDay: 0,
        changeFirstDay: false,
        beforeShow: customRange,
        buttonImage: '/Content/images/Calendar_img.png',
        buttonImageOnly: true
    });

function customRange(input) {
    var min = new Date();
    var min = new Date('<%=DateTime.Now.Year %>', '0', '1');
    var max = new Date('<%=DateTime.Now.AddYears(1).Year %>', '11', '31');

    var dateMin = min;
    var dateMax = max;

    if (input.id == "firstCal" && $("#secondCal").datepicker("getDate") != null) {
        if (dateMin < min) {
            dateMin = min;
        }
    }
    else if (input.id == "secondCal") {
        if ($("#firstCal").datepicker("getDate") != null) {
            dateMin = $("#firstCal").datepicker("getDate");
        }
    }
    return {
        minDate: dateMin,
        maxDate: dateMax
    };
}
0

All Articles