jQuery Datepicker设置动态添加的datepicker的开始和结束日期

时间:2013-02-06 23:15:18

标签: jquery jquery-ui datepicker

我动态添加了id为TrainingPeriodStart[]TrainingPeriodEnd[]的datepicker文本框。我需要针对每一对来设置正确的开始和结束日期。

开始和结束日期的设置可在文档link here中找到。

我在下面考虑了代码,但它不起作用:

    var inputName = '';
    var number = '';

    $('input[id^="TrainingPeriodStart"]').click(function() {
        inputName = $(this).attr('id');
        number = inputName.substr(inputName.length - 3); // get [number]
    }).datepicker({
        changeYear: true,
        changeMonth: true,
        dateFormat: 'yy-mm-dd',
        onSelect: function( selected ) {
            $('#TrainingPeriodEnd'+number).datepicker( "option", "minDate", 
                selected )
        }
    });

    $('input[id^="TrainingPeriodEnd"]').click(function() {
        inputName = $(this).attr('id');
        number = inputName.substr(inputName.length - 3); // get [number]
    }).datepicker({
        changeYear: true,
        changeMonth: true,
        dateFormat: 'yy-mm-dd',
        onSelect: function( selected ) {
            $('#TrainingPeriodStart'+number).datepicker( "option", "maxDate", 
                selected )
        }
    });

基本上我希望将datepicker对与[number]属性对齐,并设置其最小和最大日期,以限制用户选择非法日期范围。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

如果克隆元素,则需要向克隆元素添加新的单击事件,以便触发事件:

$(function() {
    var inputName = '';
    var number = '';

    var template = $('#training .trainingperiod:first').clone(),
        trainingPeriodCount = 1;

    var addTrainingPeriod = function(){
        trainingPeriodCount++;
        var trainingPeriod = template.clone().find(':input').each(function(){
            var newId = this.id.substring(0, this.id.length-1) + trainingPeriodCount;
            this.name = this.id = newId; // update id and name (assume the same)
        }).end() // back to .trainingperiod
        .attr('id', 'TrainingPeriod' + trainingPeriodCount) // update attendee id
        .prependTo('#training'); // add to container

        $('#TrainingPeriod' + trainingPeriodCount).find('input[id^="TrainingPeriodStart"]').datepicker({
            changeYear: true,
            changeMonth: true,
            dateFormat: 'yy-mm-dd',
            onSelect: function( selected ) {
                var thisInput = $(this).attr('id');
                var thisNumber = thisInput.substr(thisInput.length - 3);
                $('#TrainingPeriodEnd'+thisNumber).datepicker( "option", "minDate", 
                    selected )
            }
        });
        $('#TrainingPeriod' + trainingPeriodCount).find('input[id^="TrainingPeriodEnd"]').datepicker({
            changeYear: true,
            changeMonth: true,
            dateFormat: 'yy-mm-dd',
            onSelect: function( selected ) {
                var thisInput = $(this).attr('id');
                var thisNumber = thisInput.substr(thisInput.length - 3);
                $('#TrainingPeriodStart'+thisNumber).datepicker( "option", "maxDate", 
                    selected )
            }
        });        
    };

    $('.add').click(addTrainingPeriod); // attach event

    $('input[id^="TrainingPeriodStart"]').click(function() {
        inputName = $(this).attr('id');
        number = inputName.substr(inputName.length - 3); // get [number]
    }).datepicker({
        changeYear: true,
        changeMonth: true,
        dateFormat: 'yy-mm-dd',
        onSelect: function( selected ) {
            $('#TrainingPeriodEnd'+number).datepicker( "option", "minDate", 
                selected )
        }
    });

    $('input[id^="TrainingPeriodEnd"]').click(function() {
        inputName = $(this).attr('id');
        number = inputName.substr(inputName.length - 3); // get [number]
    }).datepicker({
        changeYear: true,
        changeMonth: true,
        dateFormat: 'yy-mm-dd',
        onSelect: function( selected ) {
            $('#TrainingPeriodStart'+number).datepicker( "option", "maxDate", 
                selected )
        }
    });
});

Fiddle is here