【问题标题】:Cannot get jQuery-UI dialog to open with dynamically created links无法使用动态创建的链接打开 jQuery-UI 对话框
【发布时间】:2017-01-30 20:39:11
【问题描述】:

好的,这里有个奇怪的问题。

我有以下代码:

<script>
$("#calendar_container").on("click",".day-header-link", function(e){

    refreshDialog();

    $.ajax({
        url: $(this).attr("href"),
        success: function(data){
            $(".hover-form").empty().append(data).dialog("open");
        }
    });

    return false;
});
</script>

非常简单明了,当点击的链接不是动态加载时,它就像一个魅力。当它们被动态加载时,我收到以下错误。

Uncaught TypeError: $(...).empty(...).append(...).dialog is not a function

我已经把引用的div放到了页面的非动态部分,我也用下面的函数初始化了对话框,上面的sn-p中引用了。

function refreshDialog(){
    //kick up the hover form stuff
    $(".hover-form").dialog({
        autoOpen: false,
        draggable: false,
        minWidth:350,
        minHeight: 200,
        modal: true,
        buttons: [
            {
            text: "Close",
            icons: false,
            click: function() {
            $( this ).dialog( "close" );
            }


            }
        ]
    });
}

如果相关,这里是在日历中动态加载下个月的sn-p。

$("#calendar_container").on("click","#previous_month",function(e){

    //get data from link
    res = $(this).attr("href");
    res = res.replace("schedule","ajax");
    res = res.replace("view","standalone_calendar");

    $("#calendar_container").empty();
    $("#calendar_container").html("<img src=\""+BASE_URL+"/images/loading.gif\" alt=\"loading\" />");

    $.ajax({
        async: true,
        url: res,
        method: "post",
        data: "",
        success: function(c){
            $("#calendar_container").empty().append(c);
        }
    });

    return false;
});
$("#calendar_container").on("click","#next_month",function(e){

    //get data from link
    res = $(this).attr("href");
    res = res.replace("schedule","ajax");
    res = res.replace("view","standalone_calendar");

    $("#calendar_container").empty();
    $("#calendar_container").html("<img src=\""+BASE_URL+"/images/loading.gif\" alt=\"loading\" />");

    $.ajax({
        async: true,
        url: res,
        method: "post",
        data: "",
        success: function(c){
            $("#calendar_container").empty().append(c);
        }
    });

    return false;
}); 

【问题讨论】:

  • 不确定但认为您想要“委托”事件(“on”指的是包含所有动态链接的 DOM 对象)。另见api.jquery.com/on
  • @JohnHascall 据我所知(没有标记),看起来 OP 正在委托#calendar_container 元素,点击来自day-header-link 元素。没有可验证的例子很难说。
  • #calendar_container 是页面中的静态元素,其中包含用于打开对话框的动态链接。 .hover_form 类也是页面上的静态元素。

标签: jquery jquery-ui jquery-ui-dialog


【解决方案1】:

不确定这是否是最佳解决方案,但我将对我的 refreshDialog() 函数的调用移至 ajax 成功部分,一切正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-25
    • 1970-01-01
    • 1970-01-01
    • 2012-07-16
    • 2012-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多