问题
JQuery (inside the head function in my webform):
function DoThisEM() {
centerPopupEM();
loadPopupEM();
}
function DoThatEM() {
disablePopupEM();
}
var popupStatusEM = 0;
//loading popup with jQuery magic!
function loadPopupEM() {
//loads popup only if it is disabled
if (popupStatusEM == 0) {
$("#backgroundPopupEM").css({
"opacity": "0.7"
});
$("#backgroundPopupEM").fadeIn("slow");
$("#popupContactEM").fadeIn("slow");
popupStatusEM = 1;
}
}
//disabling popup with jQuery magic!
function disablePopupEM() {
//disables popup only if it is enabled
if (popupStatusEM == 1) {
$("#backgroundPopupEM").fadeOut("slow");
$("#popupContactEM").fadeOut("slow");
popupStatusEM = 0;
}
}
//centering popup
function centerPopupEM() {
//request data for centering
var windowWidth = document.documentElement.clientWidth;
var windowHeight = document.documentElement.clientHeight;
var popupHeight = $("#popupContactEM").height();
var popupWidth = $("#popupContactEM").width();
//centering
$("#popupContactEM").css({
"position": "absolute",
"top": windowHeight / 2 - popupHeight / 2,
"left": windowWidth / 2 - popupWidth / 2
});
//only need force for IE6
$("#backgroundPopupEM").css({
"height": windowHeight
});
}
$("body").on('click', "#popupContactCloseEM", function (e) {
//e.preventDefault();
alert('popupContactCloseEM');
disablePopupEM();
});
$("body").on('click', "#backgroundPopupEM", function (e) {
//e.preventDefault();
alert('backgroundPopupEM');
disablePopupEM();
});
GridView:
Popup (when clicked on any of the edit icon:
I am not sure why but when I click on the x or the background around the popup, the disablePopupEM function is not called to close it. I even added a test alert and I am not seeing that either.
Please help me resolve the issue.
回答1:
For me that sounds very similar to this: You assign click handlers to elements which do not yet exist because the DOM did not finish loading yet (e.g. the JavaScript is initialized before your HTML).
I would try to attach the click handlers inside the $(document).ready() funtion, which will be called once the DOM is fully loaded - then the elements are available and they will be attached with your handler.
$(document).ready(function() {
$("body").on('click', "#popupContactCloseEM", function (e) {
//e.preventDefault();
alert('popupContactCloseEM');
disablePopupEM();
});
$("body").on('click', "#backgroundPopupEM", function (e) {
//e.preventDefault();
alert('backgroundPopupEM');
disablePopupEM();
});
});
As you see, you only need to surround it by that ready function.
来源:https://stackoverflow.com/questions/28908376/why-doesnt-the-pop-up-close-when-opened-from-inside-an-updatepanel