jqueryvalidatejs在第一次Ajax调用后停止工作

jqueryvalidatejs在第一次Ajax调用后停止工作,jquery,ajax,asp.net-mvc,jquery-validate,Jquery,Ajax,Asp.net Mvc,Jquery Validate,我有以下Jquery验证代码: $("#formObjective").validate({ messages: { Statement: 'Value must be provided' }, submitHandler: function (form) { try { $.post($('form#formObjective').attr('action'), $('form#formObjective'

我有以下Jquery验证代码:

$("#formObjective").validate({
    messages:
    {
        Statement: 'Value must be provided'
    },
    submitHandler: function (form) {
        try {
            $.post($('form#formObjective').attr('action'), $('form#formObjective').serializeArray())
                .done(function (data) {
                    updateObjectiveGrid('table#objectiveGrid');
                    $('form#formObjective').closest('div').html(data);
                })
                .fail(function (xhr, error, text) {
                    alert(text);
                });
        }
        catch (ex) {
            alert('An error occurred while saving: ' + ex);
        }
    }
}
这是HTML

    <div id=editor>
@using (Html.BeginForm("Save", "Objective", FormMethod.Post, new { @class = "form", id = "formObjective" }))
    {
        <fieldset>
            <legend>Add Objective</legend>
            @Html.AntiForgeryToken()
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
            @Html.HiddenFor(model => model.Id)


            <div class="form-group">
                @Html.LabelFor(model => model.Notes, "Notes", htmlAttributes: new { @class = "control-label" })
                @Html.TextAreaFor(model => model.Notes, new { @class = "form-control" } )
            </div>

            <div class="form-group">
                <button type="submit" id="save"  name="action" class="btn btn-primary btn-default">@Html.GlyphIcon("plus") Save Objective</button>
                <button type="button" id="clear" name="action" class="btn btn-warning">Clear</button>
            </div>
        </fieldset>
    }
</div>

@使用(Html.BeginForm(“Save”、“Objective”、FormMethod.Post、new{@class=“form”、id=“formObjective”}))
{
添加目标
@Html.AntiForgeryToken()
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@Html.HiddenFor(model=>model.Id)
@LabelFor(model=>model.Notes,“Notes”,htmlAttributes:new{@class=“controllabel”})
@Html.TextAreaFor(model=>model.Notes,新的{@class=“form control”})
@GlyphIcon(“plus”)保存目标
清楚的
}
现在我面临两个问题

  • 如果我将$('').validate({})放在 $(document).ready(function(){})它只是不起作用,即常规 提交发生了。如果我把它放在外面,它就会工作

  • 如果我动态地替换(相同的形式 从服务器渲染并替换现有表单),然后 validate()会停止工作,并定期进行回发


  • 您可以尝试以下代码来解决表单提交问题:

    $("#formObjective").submit(function(e) {
            e.preventDefault();
        }).validate({
        messages:
        {
            Statement: 'Value must be provided'
        },
        submitHandler: function (form) {
            try {
                $.post($('form#formObjective').attr('action'), $('form#formObjective').serializeArray())
                    .done(function (data) {
                        updateObjectiveGrid('table#objectiveGrid');
                        $('form#formObjective').closest('div').html(data);
                    })
                    .fail(function (xhr, error, text) {
                        alert(text);
                    });
                 return false;
            }
            catch (ex) {
                alert('An error occurred while saving: ' + ex);
            }
        }
    });
    

    但你认为原因是什么?如果有一些错误,那么它将永远不会工作。这个问题来自于使用此新更新防止验证表单默认提交,您需要从自定义提交处理程序返回false,如:$(“表单”).validate({submitHandler:function(form){//do some thing here return false;});直到我在提交处理程序中的e.preventDefault()之后调用$('').valid(),才开始工作。您将ajax调用中的
    替换为
    id=“formObjective”
    ,因此您处理的那一个不再存在。您需要在('submit','form',function(){…})上使用-
    $('#editor')