难以将事件处理程序附加到动态生成的模式窗口元素 [英] 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:
现在,此帖子与@StephenMuecke帖子有关,有关动态附加事件处理程序.这对我来说是新的,所以我必须阅读一下,但现在我知道它确实有意义.
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.
在阅读了文档和大量SO帖子之后,我似乎仍然无法触发click事件处理程序?
Well after reading documentation and numerous SO posts I still can't seem to get the click event handler to fire??
这次,我决定采用其他方法.我创建了一个演示该问题的jsfiddle. http://jsfiddle.net/ramjet/93nqs040/17/
This time I decided to take a different approach. I created a jsfiddle demonstrating the problem. http://jsfiddle.net/ramjet/93nqs040/17/
但是,我不得不对jsfiddle进行一些更改,以使其能够在其框架内工作.下面是实际的代码.
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>
我认为不需要模态完整的html,但如果需要,我将对其进行更新.这只是我要动态绑定的元素.
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" />
推荐答案
document
不需要引号:
$(document).on("click", "#save", function () { alert("i work");});
"document"
搜索document
的元素,而不是实际的document
"document"
searches for an element of document
, not the actual document
$("document").length; //0
$(document).length; //1
这篇关于难以将事件处理程序附加到动态生成的模式窗口元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!