这个问题是对这三个问题的持续学习/发现。我的这个问题从这里开始:
现在这篇文章是关于@StephenMuecke 关于动态附加事件处理程序的文章。这对我来说是新的,所以我不得不仔细阅读,但现在我发现它确实有道理。
在阅读文档和大量 SO 帖子后,我似乎仍然无法触发点击事件处理程序?
这次我决定采取不同的方法。我创建了一个 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,但如果需要,我会更新它。这只是我要动态绑定(bind)到的元素。
<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").length; //0
$(document).length; //1
关于javascript - 难以将事件处理程序附加到动态生成的模态窗口元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26510999/