0

对ajax来说很新。

所以我有这个div:

   <div id="authentication" title="Authentication" >
    <b>Please Generate a new token to continue!</b>
    <br /><br />
    <table>
        <tr>
            <td>Token:</td>
            <td><input type="text" id="txtToken"/></td>
        </tr>
        <tr>
            <td></td>
            <td><label id="lblError"></label></td>
        </tr>
    </table>
 </div>

它没有显示在我的 mvc 视图上,因为它被下面的 Ajax 代码用作对话框:

$('#authentication').dialog({
    autoOpen: true,
    width:500,
    resizable: false,
    beforeclose : function() { return false; },
    title: 'Authentication',
    modal: true,
    buttons: {
        "Cancel": function () {
            window.location.replace("@Url.Action("Index", "Home")");
        },
        "Submit": function () {
            var token=$('#txtToken').val();
            var dlg = $(this);
            $.ajax({
                type: 'POST',
                data: { 'token': token},
                dataType: 'json',
                url: '@Url.Action("CheckNewToken", "Account")',
                success: function (result) {
                    if(result==true)
                    {
                        window.parent.jQuery('#authentication').dialog('destroy');
                    }
                    else{
                        $('#lblError').html("Incorrect credentials. Please try again");
                    }
                },
                error: function (xhr, ajaxOptions, thrownError) { 
        }
            });
        }
    }
});

但是,当代码成功并且结果 == 结果时,对话框被破坏,但 div(对话框)然后显示在我不想要的视图上。我究竟做错了什么?

4

1 回答 1

1

关闭对话框,然后销毁。这将完全隐藏对话框,然后破坏其对话框功能。如果您只是这样做.dialog('destroy'),它将完全删除对话框功能并在页面上按原样显示元素,但不会隐藏。

success: function (result) {
                    if(result==true)
                    {
                        $('#authentication').dialog('close').dialog('destroy');
                    }
                    else{
                        $('#lblError').html("Incorrect credentials. Please try again");
                    }
                },

另一件事是beforeclose : function() { return false; },您返回 false ,这将阻止关闭事件的发生。它应该是beforeClose虽然你可以安全地删除它。

如果上述方法不起作用,则删除 div 的另一个选项是订阅close事件:-

   $('#authentication').dialog({
    autoOpen: true,
    width:500,
    resizable: false,
    title: 'Authentication',
    modal: true,
    close:function(){

        $(this).dialog('destroy').hide();
     },
    buttons: {
        "Cancel": function () {

        },
        "Submit": function () {
            var token=$('#txtToken').val();
            var dlg = $(this);
           $('#authentication').dialog('close');
        }
    }
});
于 2013-05-15T16:00:38.150 回答