繁体   English   中英

难以将事件处理程序附加到动态生成的模态窗口元素

[英]Difficulty attaching event handler to dynamically generated modal window elements

这个问题是对这三个问题的持续学习/发现。 我这个问题从这里开始:

第一篇文章

第二篇文章

现在这篇文章是关于@StephenMuecke关于动态附加事件处理程序的帖子。 这对我来说是新的,所以我不得不阅读,但现在我发现它确实有意义。

那么阅读文档和众多SO帖子之后我仍然无法让点击事件处理程序触发?

这次我决定采取不同的方法。 我创建了一个jsfiddle来演示这个问题。 http://jsfiddle.net/ramjet/93nqs040/17/

然而,jsfiddle我必须从现实中稍微改变一下才能让它在他们的框架内工作。 以下是实际代码。


用于启动模态的父窗口脚本...警报绑定会触发。

<script>

$(document).ready(function ()
{

    $("#new").click(function (e)
    {
        e.preventDefault();
        var ischanging = false;
        var financierid = 0;

        var detailsWindow = $("#window").data("kendoWindow");

        if (!detailsWindow)
        {
            // create a new window, if there is none on the page
            detailsWindow = $("#window")
                // set its content to 'loading...' until the partial is loaded
                .html("Loading...")
                .kendoWindow(
                    {
                        modal: true,
                        width: "800px",
                        height: "400px",
                        title: "@T("...")",
                        actions: ["Close"],
                        content:
                        {
                            url: "@Html.Raw(Url.Action("ActionName", "Controller"))",
                            data: { financierId: financierid, isChanging: ischanging }
                        }
                    })
                .data("kendoWindow").bind('refresh', function (e)
                {
                    alert('Bound');
                    $('document').on("click", "#save", function () { alert("i work");});
                }).center();
        }

        detailsWindow.open();


    });
</script>

我认为不需要模态完整的HTML,但如果是,我会更新它。 这只是我试图动态绑定的元素。

<input type="button" id="save" style="margin-right:10px;" value="Save Record" />

document不需要引号:

$(document).on("click", "#save", function () { alert("i work");});

"document"搜索的元素document ,而不是实际的document

$("document").length; //0
$(document).length; //1

暂无
暂无

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

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