【发布时间】:2012-02-26 23:27:13
【问题描述】:
我对 ASP.NET MVC 中的模态寡妇有一个奇怪的问题。我创建了一个负责显示模式窗口的链接。模态窗口显示并包含一个按钮 - 保存按钮。我在这个按钮上附加了一个点击事件处理程序,以便保存窗口的内容然后关闭它。整个机制看起来是这样的
// this link shows up a modal window
@Html.ActionLink("Add", "Add", "Index", null, new { id = "actionLinkAdd" });
and javaScript
$(function () {
debugger;
$("#actionLinkAdd").on("click", function (event, action) { // attaching to click event
debugger;
event.preventDefault(); //disabling default handler
var $dialog = $('<div></div>');
var $url = $(this).attr('href');
$dialog.empty();
var $kendoWindow = $dialog.kendoWindow({ //window creating
height: "200px",
title: "Dodaj",
visible: false,
width: "200px",
actions: ["Close"],
content: $url // loading of the window's content (partialView)
}).data("kendoWindow");
$kendoWindow.center();
$kendoWindow.open();
function onButtonSaveClick(event) {
debugger;
event.preventDefault();
var $url = $('#target').attr("action");
$.ajax({
url: $url,
type: "POST",
data: $('#target').serialize(),
success: function (response) {
$("#btnSave").unbind("click");
$kendoWindow.close();
$kendoWindow.destroy();
},
error: function (args) {
debugger;
$("#btnSave").unbind("click");
$kendoWindow.content(args.responseText);
}
});
}
$('#btnSave').live("click", onButtonSaveClick); // attaching to click event on save button in the modal window
});
});
一切都很好。问题是函数 onButtonClick() 被触发了几次。例如,如果我打开模态窗口两次,这个函数也将被调用两次。这对我来说很奇怪,因为我关闭了模态窗口,销毁了它,甚至解除了绑定到 btnSave 的所有单击事件(我也尝试使用 on 和 off 功能,但窗口的行为是相同的)。这种行为可能是什么原因?
【问题讨论】:
-
我认为 eventbubbling 是你的问题,btnSave 是 div 还是 href
-
阅读此链接,应该可以解决问题stackoverflow.com/questions/1536660/…
-
@kobe 是一个输入
标签: jquery asp.net-mvc model-view-controller