验证动态添加控制 [英] Validate dynamically added control
问题描述
我如何添加动态添加控件来验证?
How can I add the dynamically added control to validation?
<div class="editor-field">
@*@Html.EditorFor(model => model.Middlename)*@
<div id="x"></div>
<script type="text/javascript">
$(function () {
var newTextBoxDiv = $(document.createElement());
newTextBoxDiv.html('<input type="text" name="Middlename" id="Middlename" width="100" data-val="true" data-val-required="The Middleneim field is required." />');
newTextBoxDiv.appendTo('#x');
});
</script>
@Html.ValidationMessageFor(model => model.Middlename)
</div>
我注意到,当我不使用现成的功能,即
I noticed that when I don't use the ready function, i.e.
<script type="text/javascript">
var newTextBoxDiv = $(document.createElement());
newTextBoxDiv.html('<input type="text" name="Middlename" id="Middlename" width="100" data-val="true" data-val-required="The Middleneim field is required." />');
newTextBoxDiv.appendTo('#x');
</script>
,客户端验证踢项。有没有一种办法,明确列入deferredly创建输入验证?
,the client-side validations kicks-in. Is there a way to explicitly include the deferredly-created inputs to validations?
推荐答案
更新:这个答案是,如果你使用jQuery不显眼的验证,你没写你用什么
UPDATE: This answer is if you use jquery unobtrusive validation, you did not wrote what you use.
首先,这个插件添加到jQuery的准备:
的http://xhalent.word$p$pss.com/2011/01/24/applying-unobtrusive-validation-to-dynamic-content/
First, add this plugin to jQuery ready: http://xhalent.wordpress.com/2011/01/24/applying-unobtrusive-validation-to-dynamic-content/
记住,你必须添加表单标签内的元素。比方说,你必须在你的标记一些包装,我将其称之为元素。
Mind that you must add your element inside of form tag. Lets say that you have some wrapper around your tag, I will call it "element".
在你的元素追加到DOM,称之为:
After you append your element to DOM, call this:
$.validator.unobtrusive.parseDynamicContent($(element).find("form").selector);
var form = $(element).find("form");
$(form).valid();
$(form).find("input").each(function () {
$(this).blur(function () {
$(this).valid();
});
});
$(form).find("select").each(function () {
$(this).change(function () {
$(this).valid();
});
这篇关于验证动态添加控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!