简体   繁体   English

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

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

This question is an ongoing learning / discovery of these three questions. 这个问题是对这三个问题的持续学习/发现。 This issue for me started here: 我这个问题从这里开始:

First Post 第一篇文章

Second Post 第二篇文章

Now this post is regarding @StephenMuecke post about attaching the event handler dynamically. 现在这篇文章是关于@StephenMuecke关于动态附加事件处理程序的帖子。 This was new to me so I had to read up but now I see that it does make sense. 这对我来说是新的,所以我不得不阅读,但现在我发现它确实有意义。

Well after reading documentation and numerous SO posts I still can't seem to get the click event handler to fire?? 那么阅读文档和众多SO帖子之后我仍然无法让点击事件处理程序触发?

This time I decided to take a different approach. 这次我决定采取不同的方法。 I created a jsfiddle demonstrating the problem. 我创建了一个jsfiddle来演示这个问题。 http://jsfiddle.net/ramjet/93nqs040/17/ http://jsfiddle.net/ramjet/93nqs040/17/

However the jsfiddle I had to change somewhat from reality to get it to work within their framework. 然而,jsfiddle我必须从现实中稍微改变一下才能让它在他们的框架内工作。 Below is the actual code. 以下是实际代码。


Parent Window script that launches modal...the alert Bound does fire. 用于启动模态的父窗口脚本...警报绑定会触发。

<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>

The modal full html I didn't think was needed but if it is I will update it. 我认为不需要模态完整的HTML,但如果是,我会更新它。 This is just the element I am trying to dynamically bind to. 这只是我试图动态绑定的元素。

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

document doesn't need quotes: document不需要引号:

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

"document" searches for an element of document , not the actual document "document"搜索的元素document ,而不是实际的document

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

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

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