0

我已经动态添加了 idTrainingPeriodStart[]TrainingPeriodEnd[]. 我需要针对每一对设置正确的开始和结束日期。

开始和结束日期的设置可在此处的文档链接中找到。

我记住了下面的代码,但它不起作用:

    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 )
        }
    });

基本上我想用它们的属性来定位日期选择器对[number]并设置它们的最小和最大日期来限制用户选择非法日期范围。

我该如何解决这个问题?

4

1 回答 1

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 )
        }
    });
});

小提琴在这里

于 2013-02-11T23:39:07.693 回答