【问题标题】:Difficulty attaching event handler to dynamically generated modal window elements难以将事件处理程序附加到动态生成的模式窗口元素
【发布时间】:2014-10-22 15:18:06
【问题描述】:

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

First Post

Second Post

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

好吧,在阅读了文档和大量 SO 帖子之后,我似乎仍然无法触发 click 事件处理程序??

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

但是,我不得不对 jsfiddle 进行一些改变,以使其在他们的框架内工作。下面是实际代码。


启动模式的父窗口脚本...警报 Bound 确实会触发。

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

【问题讨论】:

    标签: javascript jquery kendo-window


    【解决方案1】:

    document 不需要引号:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多