简体   繁体   English

jQuery UI的日期选择器未出现在动态输入上

[英]jquery-ui datepicker not appearing on dynamic input

I inherited a bunch of code of varying quality and I'm fixing a bunch of things that has been wrong. 我继承了一堆质量参差不齐的代码,并且正在修复一堆错误的东西。 One thing that it does is that is has a table of form inputs, one of which uses jquery-ui Datepicker. 它要做的一件事是有一个表单输入表,其中之一使用jquery-ui Datepicker。 There is also a button that creates a new row at the bottom of the table. 表格底部还有一个按钮可创建新行。 The new row's Datepicker never shows up. 新行的日期选择器永远不会显示。

I've seen this problem listed a few times here on SO but none of those solutions worked. 我已经在SO上几次列出了此问题,但是这些解决方案均无效。 I'm putting up what I have now. 我正在整理我现在所拥有的。 My jfiddle is at: 我的jfiddle在:

http://jsfiddle.net/squarepegsys/G8WEC/2/ http://jsfiddle.net/squarepegsys/G8WEC/2/

The meat of my code is here: 我的代码的实质是:

tr.find(".datepicker").on("focusin",function() {
    console.log("got focus");
    $(this).datepicker();

}); 

In the on("focusin"...) line, I see "got focus" on the console but never see the datepicker show up. 在on(“ focusin” ...)行中,我在控制台上看到“获得焦点”,但从未看到日期选择器显示出来。

When you create the datepicker instance, jQuery assigns it an ID, and when you clone it, you're duplicating it. 创建datepicker实例时,jQuery为其分配一个ID,而克隆它时,您将对其进行复制。 Instead, create the datepicker instance dynamically via: 而是通过以下方式动态创建datepicker实例:

$(document).on('focus', ".datepicker", function () {
    $(this).datepicker( "destroy" ).datepicker();
})

jsFiddle example jsFiddle示例

This will work for dynamically created elements and jQuery will manage the IDs properly. 这将适用于动态创建的元素,并且jQuery将正确管理ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM