最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to clone jQuery Datepicker input field - Stack Overflow

programmeradmin4浏览0评论

I need to clone a jQuery datepicker input field attached a datePicker to it using the on statement.

$('form').on('click', '.datepicker', function(){
         $(this).datepicker({'changeMonth': true, 'changeYear': true, 'dateFormat': 'MM dd', 'yearRange': "<?php echo date('Y') ?>:<?php echo date('Y') + 1 ?>"}).focus();
    });

Then before cloning the input filed, I am destroying the datepicker using

$(".datepicker").datepicker('destroy').removeClass('hasDatepicker');

But still the datepicker that is being attached to then newly cloned input is messing with original input field...

Can anybody please help me on this topic?

I need to clone a jQuery datepicker input field attached a datePicker to it using the on statement.

$('form').on('click', '.datepicker', function(){
         $(this).datepicker({'changeMonth': true, 'changeYear': true, 'dateFormat': 'MM dd', 'yearRange': "<?php echo date('Y') ?>:<?php echo date('Y') + 1 ?>"}).focus();
    });

Then before cloning the input filed, I am destroying the datepicker using

$(".datepicker").datepicker('destroy').removeClass('hasDatepicker');

But still the datepicker that is being attached to then newly cloned input is messing with original input field...

Can anybody please help me on this topic?

Share Improve this question edited Oct 3, 2013 at 4:53 Praveen 56.6k35 gold badges136 silver badges166 bronze badges asked Oct 3, 2013 at 4:44 rayhanrayhan 6563 gold badges9 silver badges27 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

try this

var $clone=$(".datepicker").clone();
$clone.datepicker("destroy");
$clone.removeAttr("id");
$clone.datepicker();
$('form').append($clone);

when you clone a row it copies every thing, and when the date picker is added again it calls the input by id, you must remove the id so that the next datepicker can assign a new id to the input :)

You should destroy the datepicker for the clone element like,

var $clone=$(".datepicker").clone();
$clone.datepicker('destroy').removeClass('hasDatepicker');
$('form').append($clone);
发布评论

评论列表(0)

  1. 暂无评论