从UpdatePanel内部打开时,为什么弹出窗口不关闭

时间:2015-03-06 21:58:08

标签: javascript c# jquery asp.net gridview

JQuery(在我的webform中的head函数内):

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的:

enter image description here

弹出窗口(点击任意edit图标时:

enter image description here

我不确定为什么但是当我点击弹出窗口周围的x或背景时,不会调用disablePopupEM函数来关闭它。我甚至添加了一个测试alert,我也没有看到。

请帮我解决问题。

1 个答案:

答案 0 :(得分:1)

对我来说听起来与此非常相似:您将click handlers分配给elements但由于DOM尚未完成加载(例如JavaScript而尚未存在在HTML)之前初始化。

我会尝试将click handlers附加到$(document).ready()功能中,这将在DOM完全加载后调用 - 然后elements可用,他们将随附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();
    });
});

如您所见,您只需要通过ready函数将其包围起来。