我在ASP.NET MVC模式窗口中遇到了一個奇怪的問題。我創建了一個負責顯示模式窗口的鏈接。模態窗口顯示幷包含一個按鈕 - 保存按鈕。我將一個單擊事件處理程序附加到此按鈕以保存窗口的內容,然後關閉它。整個mechanizm看起來是這樣jQuery - 單擊事件多次觸發
// 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函數,但窗口的行爲是相同的)。什麼可能是這種行爲的原因?
我覺得事件泡泡是你的問題,什麼btnSave是它的div或href – kobe 2012-02-04 20:02:07
閱讀此鏈接,它應該解決問題http://stackoverflow.com/questions/1536660/jquery-click-event-handler-is-被稱爲兩次爲複選框 – kobe 2012-02-04 20:06:46
@kobe其輸入 – John 2012-02-04 20:17:35