对话框回发后,.hide()无法正常工作

时间:2013-04-29 11:56:30

标签: javascript jquery asp.net-mvc-4 modal-dialog

我遇到了一个jQuery对话框的问题,可能就在我面前,但我无法理解......

对话框工作正常。它显示字段并发送信息,如果是这种情况,则会返回验证消息。我的问题是,当我初始化它时,我告诉它隐藏div中的div,它确实如此。在打开时div不存在,但是如果我尝试发送信息并且它带回验证消息,则显示div。

以下是调用对话框的js:

$(function () {
$('.openDialog').click(function () {
    $.ajax({
        url: this.href,
        type: 'GET',
        cache: false,
        data: { idUsuario: $('#idUsuario').val() },
        success: function (result) {
            $('#result').html(result).dialog('open');
            $('#formButtom').hide();
        }
    });
    return false;
});

$('#result').dialog({
    autoOpen: false,
    modal: true,
    show: { effect: "drop", direction: "up" },
    dialogClass: "dialog",
    width: 400,
    resizable: false,
    position: "center",
    buttons: {
        "Confirmar": function () {
            $("form").submit();
        },
        "Cancelar":function () { $('#result').dialog('close'); }            
    }        
});    
});
 function dialogSucces(result) {
if (result.cadastro) {
    $('#result').dialog('close');        
    window.location.href = '/usuario/index';
} else {
    $('#result').html(result);
}
}

这是html:

<form id="createUsuarioForm">
    Nome
        <div class="editor-field">
            @Html.EditorFor(model => model.Nome)<br />
            @Html.ValidationMessageFor(model => model.Nome, String.Empty, new { @class = "text-error"})
        </div>
    <br />
    E-mail / Login
        <div class="editor-field">
            @Html.EditorFor(model => model.Login)<br />
            @Html.ValidationMessageFor(model => model.Login, String.Empty, new { @class = "text-error"})
        </div>
    <br />
    <div id="formButtom" class="row demo-row">
        <div class="span2">
            <input type="submit" value="Confirmar" class="btn btn-primary" />
        </div>
        <div class="span2">
            @Html.ActionLink("Cancelar", "Index", "Usuario", new { @class = "btn btn-primary" })
        </div>
    </div>
</form>

我看过这篇文章:jQuery Show/Hide not working after postback并尝试在函数中执行此操作,但它不起作用:

$(function () {
$('.openDialog').click(function () {
    $.ajax({
        url: this.href,
        type: 'GET',
        cache: false,
        data: { idUsuario: $('#idUsuario').val() },
        success: function (result) {
            $('#result').html(result).dialog('open');
            $('#formButtom').hide();
        }
    });
    return false;
    if (this.isPostback) { $('#formButtom').hide(); };
});

有谁知道我做错了什么?

1 个答案:

答案 0 :(得分:0)

您没有显示您的dialogSuccess函数是如何运行的,但我认为它也会在表单错误的情况下执行。如果是这样,您应该在那里的if else语句中添加一行。像这样:

function dialogSucces(result) {
  if (result.cadastro) {
    $('#result').dialog('close');        
    window.location.href = '/usuario/index';
  } else {
    $('#result').html(result);
    $('#formButtom').hide(); // once defected form rendered again,
                             // div hide directive should be launched again too
  }
}