【问题标题】:Why doesn't the pop up close when opened from inside an UpdatePanel为什么从 UpdatePanel 内部打开时弹出窗口不关闭
【发布时间】:2015-03-06 21:58:08
【问题描述】:

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();
});

网格视图:

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

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

请帮我解决问题。

【问题讨论】:

  • 按 control+shift+i 时控制台中是否有任何错误?
  • 我认为应该是 Page.GetType() 而不是 typeof(Page)
  • 看看这个链接中页面底部的这个例子,个人如何注册脚本以及调用关闭方法forums.asp.net/t/…
  • @MethodMan typeof(page) 一定成功了,我还在 document.ready 中添加了它们。谢谢。
  • 这是一个简单的疏忽,很多人都忽略了我过去遇到同样的问题时已经挠了几次头,但是当我看到你的代码时,我花了几分钟才看到再次...很高兴我能迅速帮助您解决问题。

标签: javascript c# jquery asp.net gridview


【解决方案1】:

对我来说,这听起来与此非常相似:您将 click handlers 分配给尚不存在的 elements,因为 DOM 尚未完成加载(例如,JavaScript 在您的 HTML 之前初始化) .

我会尝试在$(document).ready() 函数中附加click handlers,一旦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 函数将其包围即可。

【讨论】:

  • 谢谢...我会测试一下。说得通。但有一件事,为什么 PageScript 函数不能从代码隐藏中工作?
  • 这对我有用,还有 typeof(page) 和 pagescript。谢谢。
  • 很高兴知道,typeof(page) 是由@MethodMan 建议的。很高兴它现在可以工作了:-)
  • 是的,我认为两者都解决了!这太明显了,但我完全错过了。感谢您指出。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-01
  • 1970-01-01
相关资源
最近更新 更多