Jquery Unobtrusive 验证在实现 onSubmit 事件后丢失
我正在使用 ASP.NET MVC。在我将事件附加到表单的 onSubmit 事件之前,我的验证工作正常。
这就是我的表单的外观:
@using (Html.BeginForm("Recover", "Auth", FormMethod.Post, new { Id = "RecForm", onSubmit = "return Events.Submit(this,event)" }))
{
@Html.TextBoxFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
<input type="submit" value="Recover" />
}
Javascript(工作正常,只需将其放在这里,以便您可以检查其中是否有某些内容导致验证不触发):
//Events
window.Events = (function () {
// Baseline setup
var Events = {};
function disableAllChildren(el) {
$(el).find(':input').attr('disabled', true);
}
function enableAllChildren(el) {
$(el).find(':input').attr('disabled', false);
}
//Ajax submit
Events.Submit = function (el, event) {
event.preventDefault();
//serialize the form before disabling the elements.
var data = $(el).serialize();
disableAllChildren(el);
$(el).find('.success, .error').hide();
$.ajax({
url: el.action,
type: 'POST',
data: data,
success: function (data) {
//Stuff happens.
}
});
};
return Events;
})(this, this.document);
如何取回我的验证?
I'm using ASP.NET MVC. My validation works well until I attached an event to the form's onSubmit event.
This is how my form looks:
@using (Html.BeginForm("Recover", "Auth", FormMethod.Post, new { Id = "RecForm", onSubmit = "return Events.Submit(this,event)" }))
{
@Html.TextBoxFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
<input type="submit" value="Recover" />
}
The Javascript (works fine, just put it here so you can check if something in this is causing the validation not to fire):
//Events
window.Events = (function () {
// Baseline setup
var Events = {};
function disableAllChildren(el) {
$(el).find(':input').attr('disabled', true);
}
function enableAllChildren(el) {
$(el).find(':input').attr('disabled', false);
}
//Ajax submit
Events.Submit = function (el, event) {
event.preventDefault();
//serialize the form before disabling the elements.
var data = $(el).serialize();
disableAllChildren(el);
$(el).find('.success, .error').hide();
$.ajax({
url: el.action,
type: 'POST',
data: data,
success: function (data) {
//Stuff happens.
}
});
};
return Events;
})(this, this.document);
How do I get back my validations?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我所做的就是在提交之前检查表格是否有效。请参阅下面的代码:
如果表单有效,则发布您的数据。如果没有,则什么都不做。
What I do is to check if the form is valid before I submit it. See the below code :
If the form is valid, then post your data. If not, do nothing.
您的代码中存在比验证更多的问题。情况是
disabled
字段不会发布到服务器。他们也不参与验证。因此,即使您的代码有效,您也不会收到任何内容发布到服务器端。删除disableAllChildren(el);
或将其重新编码为不禁用字段,但可能使它们只读。这样,它们就会得到验证,但在 ajax 提交之后也就不足为奇了。这是您不希望出现的行为,因此请查看验证方法。尤其是你应该对它的部分感兴趣
我相信通过 ajax 验证表单提交可以更轻松地完成。
或者,只需在 ajax 提交之前添加验证检查
There're more problems in your code than validation. Case is that
disabled
fields do not get posted to server. They do not participate in validation either. So, even if your code worked, you would get nothing posted to server side. RemovedisableAllChildren(el);
or recode it to not disable fields, but maybe make them readonly.With this, they will get validated, but not surprisingly after the ajax submit. This is behavior you should not wish, so take a look at validate method. Especially you should be interested in part where it
I believe that way validated form ajax submission can be done easier.
Or, just add validation checking right before ajax submit