0

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

对话框工作正常。它显示字段并发送信息,如果是,则返回验证消息。我的问题是,当我初始化它时,我告诉它在 if 中隐藏一个 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(); };
});

有谁知道我做错了什么?

4

1 回答 1

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
  }
}
于 2013-04-29T13:18:26.120 回答