Ajax请求后手动绑定JQuery验证

发布于 2024-11-14 11:07:40 字数 195 浏览 5 评论 0 原文

我正在请求一个实时框的 ASP.net MVC 视图,该视图包含已用 JQuery 的非侵入式验证器插件使用的属性标记的表单字段。

然而,客户端脚本不起作用,我的理论是,这是因为验证框架仅在页面加载时触发,而当 MVC 视图加载到实时框中时,页面加载早已过去。

因此,我如何让验证框架知道它有新的表单字段需要修复?

干杯,伊恩。

I'm requesting an ASP.net MVC view into a live box and the view contains form fields that have been marked up with attributes to be used by JQuery's unobtrusive validators plug-in.

The client script is not however working and my theory is that its because the validation framework is only being triggered on page load which has long since passed by the time the MVC view has been loaded into the live box.

Thus how can I let the validation framework know that it has new form fields to fix up?

Cheers, Ian.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

橪书 2024-11-21 11:07:40
var $form = $("form");

$form.unbind();
$form.data("validator", null);

$.validator.unobtrusive.parse(document);
// Re add validation with changes
$form.validate($form.data("unobtrusiveValidation").options);
var $form = $("form");

$form.unbind();
$form.data("validator", null);

$.validator.unobtrusive.parse(document);
// Re add validation with changes
$form.validate($form.data("unobtrusiveValidation").options);
赢得她心 2024-11-21 11:07:40

您可以查看以下博客文章。这是另一个

You may take a look at the following blog post. And here's another one.

百合的盛世恋 2024-11-21 11:07:40

另一种选择,相当技巧,对我有用。只需在 ajax 调用返回的部分视图的开头添加以下行

this.ViewContext.FormContext = new FormContext(); 

参考

Another option, rather trick, which worked for me. Just add following line in the beginning of the partial view which is being returned by ajax call

this.ViewContext.FormContext = new FormContext(); 

Reference

﹏雨一样淡蓝的深情 2024-11-21 11:07:40

由于某种原因,我必须结合 bjan 和 dfortun 的答案......

所以我把这个放在我的观点中:

@{
  this.ViewContext.FormContext = new FormContext();
}

这在 ajax 调用完成后执行:

var form = $("#EnrollmentForm");
form.unbind();
form.data("validator", null);
$.validator.unobtrusive.parse(document);
form.validate(form.data("unobtrusiveValidation").options);

For some reason I had to combine bjan and dfortun's answers...

So I put this in my view:

@{
  this.ViewContext.FormContext = new FormContext();
}

And this execute this after the ajax call finishes:

var form = $("#EnrollmentForm");
form.unbind();
form.data("validator", null);
$.validator.unobtrusive.parse(document);
form.validate(form.data("unobtrusiveValidation").options);
绿光 2024-11-21 11:07:40

我有类似的问题。我有一个表单,它使用 Ajax 请求重新显示具有不同表单字段的表单的一部分。 在客户端手动执行此操作来使用不引人注目的验证

@Html.TextBoxFor

我通过使用文本框 。由于某种原因,当尝试使用无效字段提交时,验证会起作用(例如,文本框以红色勾勒,并且相应的错误消息会显示我在属性中放入的内容

data_val_required

)。

但是,在我单击一个按钮后,一个 Ajax 请求修改具有不同字段的表单,然后再次提交,仅显示无效字段上的红色轮廓,但没有呈现任何错误消息

bjan 的技巧对我有用,但我仍然看不到导致问题的原因。执行客户端所需的所有 HTML我只是不明白为什么错误消息属性值不会显示。

我所能想到的是 jQuery 验证代码在提交后不会再次尝试检查表单字段。

I had a similar issue. I had a form that was using Ajax requests to re-display a part of the form with different form fields. I used unobtrusive validation by manually doing it on the client side using the

@Html.TextBoxFor

for my text boxes. For some reason the validation works when attempting to submit with invalid fields (i.e., the text boxes get outlined in red and the appropriate error messages display with the content I put in the

data_val_required

attribute, for example.

However, after I click a button that makes an Ajax request to modify the form with different fields and then submit again, only the red outline on the invalid fields display, but no error messages are rendered.

bjan's trick worked for me, but I still can't see what was causing the issue. All the HTML necessary to carry out the client-side validation was there I just can't figure out why the error message attribute values wouldn't display.

All I can think of is that the jQuery validation code doesn't make a second attempt to check the form fields after a submit was made.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文