Difficulty attaching event handler to dynamically generated modal window elements

浪子不回头ぞ 提交于 2019-12-11 07:47:40

问题


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

This time I decided to take a different approach. I created a jsfiddle demonstrating the problem. http://jsfiddle.net/ramjet/93nqs040/17/

However the jsfiddle I had to change somewhat from reality to get it to work within their framework. 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. This is just the element I am trying to dynamically bind to.

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

回答1:


document doesn't need quotes:

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

"document" searches for an element of document, not the actual document

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


来源:https://stackoverflow.com/questions/26510999/difficulty-attaching-event-handler-to-dynamically-generated-modal-window-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!