Pergunta

I have a form, which first loads text fields and has a Time Picker associated with it. The user can also click a 'More' button, which dynamically loads extra text fields underneath. This also has a time picker associated with it, however it will not load on the dynamic fields. I am using a jQuery timepicker addon by Trent Richardson http://trentrichardson.com/examples/timepicker

I also found a previous answer (https://stackoverflow.com/questions/18666634/jquery-timepicker-doesnt-work-on-dynamic-input), however this does not fix my problem.

This is the form; the first div is loaded dynamically:

<div class="row">
<a href="#more" rel="[{$day}]" id="addhour_{$outlet}_{$day}" class="btn-more add-hours-link">Add More+</a>
<div class="sample" style="display: none;">
    <label>&nbsp;</label>
    <input type="text" class="outlet-hour from timepicker2" name="from[{$day}][]" />
    <span>to</span>
    <input type="text" class="outlet-hour to timepicker2" name="to[{$day}][]" />
</div>


<div>
    <label for="from_{$outlet}_{$day}">{$name}</label>
    <input id="from_{$outlet}_{$day}" class="outlet-hour from timepicker" type="text" name="from[{$day}][]" />
    <span>to</span>
    <input id="to_{$outlet}_{$day}" type="text" class="outlet-hour to timepicker" name="to[{$day}][]" />
</div>

This is the JS for the main fields (which is working fine):

$('.timepicker').timepicker({
    showSecond: false,
    timeFormat: "H:mm",
    stepMinute: 15
});

This is the JS that adds the extra fields:

$(document).on('click', 'a.add-hours-link', function(event, hour){
    var from = !!hour ? hour.from : '';
    var to = !!hour ? hour.to : '';

    $('.timepicker2').removeClass('hasDatepicker');
    $('.timepicker2').each(function(){
    $(this).timepicker({
        showSecond: false,
        timeFormat: "H:mm",
        stepMinute: 15
    });
});

    $(this).parent().find('.sample')
        .clone()
        .removeClass('sample')
        .appendTo($(this).parent())
        .find('.from').val(from).end()
        .find('.to').val(to).end()
        .fadeIn('fast');

    return false;
});

Currently I have renamed the class to timePicker2 and am removing the hasDatepicker class, as mentioned in a previous answer.

Any ideas appreciated. Thanks

Foi útil?

Solução

You need to apply the timepicker function after you have added to the DOM:

$(document).on('click', 'a.add-hours-link', function(event, hour){
    var from = !!hour ? hour.from : '';
    var to = !!hour ? hour.to : '';

    $(this).parent().find('.sample')
        .clone()
        .removeClass('sample')
        .appendTo($(this).parent())
        .find('.from').val(from).end()
        .find('.to').val(to).end()
        .fadeIn('fast');

        $('.timepicker2').removeClass('hasDatepicker');
        $('.timepicker2').each(function(){
        $(this).timepicker({
        showSecond: false,
        timeFormat: "H:mm",
        stepMinute: 15
        });
    });

    return false;
});
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top