Cloning JQuery UI datepicker

本文详细介绍了如何解决使用jQuery UI日期选择器时遇到的克隆问题,包括通过手动调用datepicker('destroy')方法、或者在克隆前先销毁原有日期选择器来解决相关错误。
In case you were having problems with cloning fields with  jQuery UI datepicker  attached to them - solutions  mentioned   in   the   interwebs  are similar to this one:
1
$( '.cloned-input' ).removeClass( 'hasDatepicker' ).datepicker();
However, that did not work for me. If you happen to have a set of similar symptoms: 
  • new datepicker is not instantiated at all
  • JS errors occur while instantiating new datepicker
  • even if datepicker is cloned, it refers to the old field
the issue is that there are remaining (cloned) events (if you're using  .clone(true)  like me) on the field AND there is a still attached cloned datepicker object.

SOLUTION

Either imitate  datepicker('destroy')  manually:
1
2
3
4
5
6
7
8
9
$input = $( '.cloned-input' );
// remove still present related DOM objects
$input.siblings( '.ui-datepicker-trigger,.ui-datepicker-apply' ).remove();
// remove datepicker object and detach events
$input
   .removeClass( 'hasDatepicker' )
   .removeData( 'datepicker' )
   .unbind()
   .datepicker();
or implement a different procedure:
  1. before cloning destroy the datepicker on the base input
  2. clone(true)
  3. recreate the datepicker on base input
  4. use unbind() and recreate datepicker on cloned input


转载于:https://my.oschina.net/williambao/blog/42216

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值