在 AJAX 和使用 MVC 3 和 jQuery 动态创建之后,元素未通过不引人注目的验证进行验证
这是我的问题...
我有一个页面最初呈现并显示使用 Ajax.BeginForm 创建的 AJAX 表单。输入一些条件并发布 AJAX 表单。返回一个部分视图,其中包含使用 Html.BeginForm 创建的 HTML 表单。
到目前为止一切都很好...到目前为止一切都很好。我的不显眼的客户端验证对于从部分视图创建的元素效果很好。当我现在尝试创建一些动态输入元素(通过 jQuery)并将它们插入/附加到部分视图创建的表单中时,我的问题发生了。我无法在动态创建的元素上进行任何不显眼的客户端验证。
我尝试过: - 克隆现有元素,修改它们的 id/名称并将它们附加到 DOM 中。 - 创建 html 字符串并将它们插入到 DOM 中。
每次插入动态元素后,我尝试调用: - $.validator.unobtrusive.parseElement() [新元素] - $.validator.unobtrusive.parse() [新容器、文档等] - $.validator.unobtrusive.parseDynamicContent() [插件]
这里是 jQuery 代码详细信息...
$("#insert").live("click", function () {
var html = "<input type='text' value='' name='CustomerNominationVolume' id='CustomerNominationVolume' data-val-required='The Nomination (Dth) field is required.' data-val-number='The field Nomination (Dth) must be a number.' data-val='true' /><br />"
html += "<span class='field-validation-valid' data-valmsg-replace='true' data-valmsg-for='CustomerNominationVolume'></span>";
$("#new").html(html);
});
$("#parse").live("click", function () {
$.validator.unobtrusive.parse(document);
});
$("#validate").live("click", function () {
var element = $("#CustomerNominationVolume");
var form = $(element).first().closest('form');
form.validate().element(element);
});
任何帮助将不胜感激!
编辑: 在做了一些额外的研究和测试后,我回到了这个 解决方案来确定为什么它对我不起作用。事实证明,我正在插入属于列表一部分的新输入元素。因此,我的新元素的名称看起来类似于:something[0].id、something[1].id 等...
这些名称导致 $.validator.unobtrusive.parseDynamicContent() 函数在此方面失败线: $('[name=' + elname + ']').rules("add", args);
我只是将其更改为 $("[name='" + elname + "']").rules("add", args);
现在一切都工作正常......
Here's my problem...
I have a page that initially renders and displays an AJAX form created using Ajax.BeginForm. Some criteria is entered and the AJAX form is posted. A partial view is returned which contains an HTML form created using Html.BeginForm.
So far so good... Everything to this point works fine. My unobtrusive client validation works great on the elements created from the partial view. My problem happens when I now try to create some dynamic input elements (via jQuery) and insert/append them into the form created by the partial view. I cannot get any unobtrusive client validation to occur on my dynamically created elements.
I have tried:
- cloning existing elements, modifying their ids/names and appending them in the DOM.
- creating html strings and inserting them in the DOM.
After each insert of the dynamic element I have tried calling:
- $.validator.unobtrusive.parseElement() [new element]
- $.validator.unobtrusive.parse() [new container, document, etc]
- $.validator.unobtrusive.parseDynamicContent() [plug-in]
Here are the jQuery code details...
$("#insert").live("click", function () {
var html = "<input type='text' value='' name='CustomerNominationVolume' id='CustomerNominationVolume' data-val-required='The Nomination (Dth) field is required.' data-val-number='The field Nomination (Dth) must be a number.' data-val='true' /><br />"
html += "<span class='field-validation-valid' data-valmsg-replace='true' data-valmsg-for='CustomerNominationVolume'></span>";
$("#new").html(html);
});
$("#parse").live("click", function () {
$.validator.unobtrusive.parse(document);
});
$("#validate").live("click", function () {
var element = $("#CustomerNominationVolume");
var form = $(element).first().closest('form');
form.validate().element(element);
});
Any help would be GREATLY appreciated!
EDIT:
After doing some additional research and testing I went back to this solution to determine why it wasn't working for me. As it turns out I was inserting new input elements that were part of a list. Therefore, the name of my new elements would look similar to this: something[0].id, something[1].id, etc...
These names caused the $.validator.unobtrusive.parseDynamicContent() function to fail on this line:
$('[name=' + elname + ']').rules("add", args);
I simply changed this to
$("[name='" + elname + "']").rules("add", args);
Everything is now working fine...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
添加动态内容后,请调用:
其中“$('form')”是表单的选择器。
那应该可以解决问题。
Once you've added your dynamic content, then call:
Where "$('form')" is a selector for your form.
That should do the trick.