关于使用 jBox 对话框的提交问题

jBox 是个不错的对话框组件。

在 ASP.NET Form 中使用 jBox 的时候,在按钮注册的客户端点击事件中,会发现不能弹出对话框问题。

表现为页面一闪就提交了,导致对话框一闪而过,甚至根本看不到。导致模式对话框失败。

首先,按钮会有默认处理,对于普通的 ASP.NET 按钮来说,会导致表单的提交,提交表单导致了页面的刷新。所以,为了不提交表单,就需要阻止按钮默认的行为,这可以通过下面的代码实现。

function stopDefault( e ) {
    // Prevent the default browser action (W3C)
    if ( e && e.preventDefault )
        e.preventDefault();
    else
        // A shortcut for stoping the browser action in IE
        window.event.returnValue = false;
    return false;
}

其次,在关闭对话框的时候,我们希望能够提交表单,这也可以通过脚本来实现。就是调用表单对象的提交方法 submit();

在实现中,我们还需要找到控件的客户端标识,可以如下获取

// 取得需要处理的按钮的 id 串
var btnSaveId = "<%= this.btnSave.ClientID %>";
var form1Id = "<%= this.form1.ClientID %>";

按钮点击的客户端处理如下

// 注册按钮的点击事件处理
$("#" + btnSaveId).click(function ( e ) {
                    
    // 设置在关闭对话的时候提交表单
    var options = {
        closed: function () {
            alert("submit");
            // 找到需要提交的表单
            $("#" + form1Id ).submit();
        }
    };

    // 显示 jBox 对话框
    var info = 'jQuery jBox<br /><br />版本:v2.0<br />日期:2011-7-24<br />';
    info += '官网:<a target="_blank" href="http://kudystudio.com/jbox">http://kudystudio.com/jbox</a>';
    $.jBox(info, options );

    // 阻止默认的事件处理
    stopDefault(e);

});
对于 jQuery 来说,在事件处理方法中返回 false 可以完成类似功能。
但是这两种方式是有区别的。return false 不仅阻止了事件往上冒泡,而且阻止了事件本身。
stopDefault 则只阻止默认事件本身,不阻止事件冒泡。
还可以阻止事件冒泡,这需要调用下面的方法。
function stopBubble(e) {
    // If an event object is provided, then this is a non-IE browser
    if (e && e.stopPropagation)
        // and therefore it supports the W3C stopPropagation() method
        e.stopPropagation();
    else
        // Otherwise, we need to use the Internet Explorer
        // way of cancelling event bubbling
        window.event.cancelBubble = true;
}
原文地址:https://www.cnblogs.com/haogj/p/2754303.html