0

这些是我的 javascript 函数,用于检查用户是否在不保存数据的情况下退出应用程序

 <script src="jquery-1.4.2.min.js" type="text/javascript"></script>
<script src="JScript.js" type="text/javascript"></script>
<script src="JScript2.js" type="text/javascript"></script>
<script src="jquery.alerts.js" type="text/javascript"></script>
<link href="jquery.alerts.css" rel="stylesheet" type="text/css" media="screen" />

<script type="text/javascript">
    var isDirty;
    isDirty = 0;

    function setDirty() {
        var form = document.getElementById('form1');
        var inputs = form.getElementsByTagName('input');
        var e = document.getElementById("ddl");
        var strUser = e.options[e.selectedIndex].value;
        var elems = document.getElementsByTagName('input');
        for (var i = 0; i < elems.length; i++) {
            if (elems[i].type == 'text' || elems[i].type == 'checkbox' || elems[i].type == 'radio') {
                is_checked = elems[i].checked;
                is_checked1 = elems[i].checked;
                if (elems[i].value == '' && !(is_checked) && !(is_checked1) && strUser == '0') {
                    isDirty = 0;
                    //alert("In");
                }
                else {
                    isDirty = 1;
                }
            }
        }
    }
    $(document).ready(function f() {
        alert("In");
        $("#Button1").click(function () {
            if (isDirty == 1) {
                jConfirm('Can you confirm this?', 'Confirmation Dialog', function (r) {
                    if (r == true) {
                        //var link = '<%=btnDelete.UniqueID %>'
                        //__doPostBack('<%=btnDelete.UniqueID%>', "");
                    }
                    else
                        return false;

                });
                //return false;
                return false;
            }
            else {
                // __doPostBack('<%=btnDelete.UniqueID%>', "");
            }
        });
    });



</script>
<script type="text/javascript">

    $(window).bind('beforeunload', function () {
        if (isDirty == 1) {
            alert("Hi");
            jConfirm('Can you confirm this?', 'Confirmation Dialog', function (r) {
                if (r == true) {
                    //var link = '<%=btnDelete.UniqueID %>'
                    //__doPostBack('<%=btnDelete.UniqueID%>', "");
                }
                else
                    return false;

            });
            //return false;
            return false;
        }
        else {
            // __doPostBack('<%=btnDelete.UniqueID%>', "");
        }
    });

</script>

这是我的设计

<div>
    <table>
        <tr>
            <td>
                <asp:TextBox ID="txt" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            </td>
            <td>
                <asp:RequiredFieldValidator ID="Rqrd" runat="server" ValidationGroup="g" ControlToValidate="TextBox1"
                    ErrorMessage="Required"></asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td>
                <asp:CheckBox ID="chk" runat="server" Text="Check Me" />
            </td>
        </tr>
        <tr>
            <td>
                <asp:RadioButton ID="radio" runat="server" Text="Radio" />
            </td>
        </tr>
        <tr>
            <td>
                <asp:DropDownList ID="ddl" runat="server">
                    <asp:ListItem Text="--Select--" Value="0"></asp:ListItem>
                    <asp:ListItem Text="One" Value="1"></asp:ListItem>
                    <asp:ListItem Text="Two" Value="2"></asp:ListItem>
                </asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td>
                <input id="Button2" type="button" value="Submit" />
            </td>
            <td style="display: none; font-size: 0px; width: 0px;">
                <asp:LinkButton ID="btnDelete" runat="server" Text="Ok" ValidationGroup="g" OnClick="btn1_Click" />
            </td>
            <td>
                <asp:Button ID="Button1" runat="server" Text="Cancel" OnClick="Button1_Click" />
            </td>
        </tr>
    </table>
    <%--<asp:Button ID="btn1" runat="server" Text="Ok" onclick="btn1_Click" />--%>
</div>

在取消点击时,我得到了我需要的警报,当我关闭出错的标签时没有得到同样的警报,有人可以帮助我吗

我的自定义警报需要这种场景

http://www.mkyong.com/wp-content/uploads/jQuery/jQuery-stop-page-from-exit.html

在此处输入图像描述

4

1 回答 1

1

EDITED删除了两行测试代码

 function checkit(){


    if (isDirty == 1) {

            jConfirm('Can you confirm this?', 'Confirmation Dialog', function (r) {

                if (r === true) {

                //var link = '<%=btnDelete.UniqueID %>';
                //__doPostBack('<%=btnDelete.UniqueID%>', "");

                }else{

                return false;

                }

            });

    return false;

    }else{

       // __doPostBack('<%=btnDelete.UniqueID%>', "");

    }

}   

然后用这些中的每一个调用函数?

 $("#Button1").click(checkit);

 $(window).bind('beforeunload',checkit);
于 2012-11-02T13:59:39.313 回答