DatePicker无法在ajax加载的页面中工作

ozg*_*krc 2 ajax jquery jquery-ui

我将datepicker附加到全局脚本文件中的输入,如下所示:

$(document).on("focusin",".datePick", function () {
            $(this).datepicker({
                dateFormat: "dd/mm/yy",
                changeMonth: true,
                changeYear: true,
                onClose: function () { $(this).valid(); }
            });
        });
Run Code Online (Sandbox Code Playgroud)

在特定页面上,在模态对话框(也是jquery ui)中有输入(将与datepicker一起使用),我通过$ .load()调用该页面并在其他页面中注入div.

上面的代码非常适用于其他页面中的静态输入,但是对于上面的场景,它显示了datepicker对话框,但是当我点击日期时,它会抛出错误(f未定义)编辑:VS2010抛出"无法设置值"属性'currentDay':对象为null或未定义"

有人建议使用live()方法,但我不想使用弃用方法.我该怎么办?

提前致谢

Ozgu

ozz*_*ozz 10

我遇到了这个确切的错误,虽然我的答案对OP没有帮助,但也许它将来会帮助其他人.

当我与页面中的其他元素发生ID冲突时收到此错误消息.


Met*_*rog 6

正如我在对 TJ VanToll 的回答的评论中提到的,只要在加载 DOM 时存在触发器绑定到的父元素,就可以了。

有关示例,请参阅此小提琴

JS:

$(function(){
    $(document).on("focusin",".datePick", function () {
       $(this).datepicker({
            dateFormat: "dd/mm/yy",
            changeMonth: true,
            changeYear: true,
            onClose: function () { $(this).valid(); }
        });
    });

    $('#focusin').append('<input class="datePick" name="datepicker" />');

});?
Run Code Online (Sandbox Code Playgroud)

HTML:

<div id="focusin"></div>?
Run Code Online (Sandbox Code Playgroud)

只要您的模态 div 在加载时存在,它就应该能够在加载后捕获您的输入。