jQuery不显眼的验证在无效时返回有效

jQuery unobstrusive validation returning valid when it is not

本文关键字:返回 有效 无效 不显眼 验证 jQuery      更新时间:2023-09-26

我有一个表单,它有一个必需的输入,但是jQuery说这个字段是有效的,当它不是。这很奇怪,因为在HTML中它看起来像是有价值的。

这是Chrome控制台中三个命令的输出,文本框完全为空:

// First I ensure that the selector is correct. But I can see that it has value, 
// when it is actually empty.
$('#form_0 input[name=Name]')[0].outerHTML
"<input data-val="true" data-val-length="The field Customer name must be a string with a minimum length of 3 and a maximum length of 20." data-val-length-max="20" data-val-length-min="3" data-val-required="The Customer name field is required." id="Name" name="Name" type="text" value="             Customer 0         " class="valid">"
// jQuery confirms that the field is empty
$('#form_0 input[name=Name]').val()
""
// but jQuery say that the control is valid :?
$('#form_0 input[name=Name]').valid()
1

表单和输入都是用jQuery动态创建的。

有什么问题吗?

谢谢。

$.validator.unobtrusive.parse('#form_0');

我不知道当我动态添加输入时,我必须再次调用它。我想我必须阅读更多的文档和更少的例子:P