我有以下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>
现在我面临着两个问题。
如果我将$('')。validate({})放在 $(document).ready(function(){})它只是不起作用,即常规 提交发生。如果我把它放在外面,它就可以了。
如果我动态替换&lt; form&gt; (相同的表单) 从服务器渲染并替换现有的表单),然后是 validate()只是停止工作并发生定期回发。
答案 0 :(得分:1)
您可以尝试使用此代码来修复表单提交问题:
$("#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);
}
}
});