Difficulty attaching event handler to dynamically

2019-08-06 11:27发布

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条回答
小情绪 Triste *
2楼-- · 2019-08-06 11:43

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
查看更多
登录 后发表回答