【问题标题】:onClientClick executes and then the page postbacks immediatelyonClientClick 执行,然后页面立即回发
【发布时间】:2011-07-20 21:54:03
【问题描述】:

我有以下代码:

functions.js =>

function loadPopup() {
    $("#backgroundPopup").css({
        "opacity": "0.7"
    });
    $("#backgroundPopup").fadeIn("slow");
    $("#popupContact").fadeIn("slow");
}

//disabling popup with jQuery magic!  
function disablePopup() {
    $("#backgroundPopup").fadeOut("slow");
    $("#popupContact").fadeOut("slow");
}

//centering popup  
function centerPopup() {
    //request data for centering  
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = $("#popupContact").height();
    var popupWidth = $("#popupContact").width();
    //centering  
    $("#popupContact").css({
        "position": "absolute",
        "top": windowHeight / 2 - popupHeight / 2,
        "left": windowWidth / 2 - popupWidth / 2
    });
    //only need force for IE6  

    $("#backgroundPopup").css({
        "height": windowHeight
    });

}

function no() {
    return false;
    disablePopup();
}

function yea() {
    $('#form1').submit(function () {
        return true;
    })
}

$(function () {
    $("#popupContactClose").bind('click', function () {
        disablePopup();
    });
});

function yeah() {
    $(this).bind('click', function (event) {

        centerPopup();

        loadPopup();

    });
}

...以及以下标记:

<body>
    <form id="form1" runat="server">
    <asp:Button ID="btn1" runat="server" OnClientClick="yeah();" Text="Click me!" />
    <div id="popupContact">
        <a onclick="xClick()" id="popupContactClose">x</a>
        <input type="submit" onclick="yea()" value="Yes" />
        <input type="button" onclick="no()" value="No" />
    </div>
    <div id="backgroundPopup">
    </div>
    </form>
</body>
</html>

问题是 OnClientClick 事件调用 yea(),div 出现然后立即消失,页面回发。这很奇怪。你能帮我解决这个问题吗?提前致谢!

【问题讨论】:

    标签: jquery asp.net frontend


    【解决方案1】:

    我认为你需要扼杀这个事件。

    onclick="yea();return false;"
    

    可能会这样做。

    【讨论】:

    • 虽然考虑到你添加到前一个试图回答这个问题的人中的内容可能也不会满足你的要求。在这种情况下,您可能需要在 yea() 中通过 javascript 提交表单。
    • 我猜你是对的。您在此处提供的解决方案无效。它确实触发了一个帖子,但是我的服务器端代码没有执行.. :/ 有什么建议吗?顺便说一句,感谢您花时间回答我的问号
    • 我以为常见的做法是function yea(e){/*user code*/e.preventDefault();} onclick="yea(event);"
    • Pencho,你有什么建议?我对javascript和jquery不太熟悉。我正试图快速捡起它,但我在这里撞到了石墙.. 帮助 wud 不胜感激
    猜你喜欢
    • 1970-01-01
    • 2015-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多