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

发布于 2024-11-27 20:45:15 字数 865 浏览 2 评论 0原文

我有一个包含必需输入的表单,但 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 动态创建的。

可能是什么问题?

谢谢。

I have a form that has a required input, but jQuery is saying that the field is valid when it is not. It is strange, because the by the HTML it looks like it has value.

This is the output of three commands in the Chrome console with the textbox totally empty:

// 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

The form and the inputs have been created dynamically with jQuery.

What could be the problem?

Thanks.

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

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

发布评论

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

评论(1

苹果你个爱泡泡 2024-12-04 20:45:15
$.validator.unobtrusive.parse('#form_0');

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

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

I didn't know I have to call it again when I add inputs dynamically. I guess I have to read more documentation and less examples :P

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