Datepicker和timepicker不处理附加元素

时间:2012-05-22 12:02:27

标签: jquery

我正在使用datepickertimepicker这样的字段:

<input class="datepicker" name="sugg_date[]" type="text" value="" />
<input class="tp" name="sugg_time[]" type="text" value="" />

我也在使用此功能添加更多行:

<script>
    function add_next_row(e, row_a) {
        curr_row = $(row_a).prev('p');
        curr_row.append('<input class="datepicker" name="sugg_date[]" type="text" value="" />');
        curr_row.append('<input class="tp" name="sugg_time[]" type="text" value="" />');

        if (e.preventDefault) {
            e.preventDefault();
        } else {
            e.returnValue = false;
        }
    }
</script>

但新行不适用于timepickerdatepicker。有人可以告诉我原因。

2 个答案:

答案 0 :(得分:1)

您需要为添加的每个新元素初始化datepickertimepicker。试试这个:

function add_next_row(e, row_a) {
    e.preventDefault();

    var $datefield = $('<input class="datepicker" name="sugg_date[]" type="text" value="" />').datepicker();
    var $timefield = $('<input class="tp" name="sugg_time[]" type="text" value="" />').timepickr({convention:12}); 

    curr_row = $(row_a).prev('p');
    curr_row.append($datefield);
    curr_row.append($timefield);
}

答案 1 :(得分:1)

    function add_next_row(e, row_a) {

    if (e.preventDefault) {
        e.preventDefault();
    } else {
        e.returnValue = false;
    }
    curr_row = $(row_a).prev('p');
    var $datefield = $('<input class="datepicker" name="sugg_date[]" type="text" value="" />').datepicker();
    var $timefield = $('<input class="timepickr" name="sugg_time[]" type="text" value="" />');
    curr_row.append($datefield);
    curr_row.append($timefield);
    curr_row.append("<br/>");


    $(".timepickr").timepickr().timepickr({convention:12});


}