gpt4 book ai didi

jquery - 对隐藏字段执行验证

转载 作者:行者123 更新时间:2023-12-03 21:54:40 25 4
gpt4 key购买 nike

我有以下观点:

    @Html.LabelFor(m => m.CompanyPostCode)
@Html.TextBoxFor(m => m.CompanyPostCode)

@Html.LabelFor(m => m.CompanyCity)
@Html.TextBoxFor(m => m.CompanyCity)

@Html.HiddenFor(m => m.CompanyCityID)

所有属性在我的 View 模型中都标记为[必需]。那么问题是我的 CompanyCityID(标记为必需)被隐藏,因此 View 中没有进行验证。如果我在 View 中显示此属性,则验证已完成。

enter image description here

我的问题:是否可以对隐藏字段执行验证?存在解决方法吗?

验证隐藏字段可能看起来有点奇怪。原因是该字段是由 jQuery 根据特殊规则填充的。如果未填充,我就知道 View 上的某些内容无效。

谢谢。

最佳答案

可能的原因是jquery.validate.unobtrusive.js文件中有ignore: ':hidden'行。

1.9.0版本之后这是默认行为。您可以通过添加手动修复该问题

$.validator.setDefaults({ ignore: [] });

如您所见here

Another change should make the setup of forms with hidden elements easier, these are now ignored by default (option “ignore” has “:hidden” now as default). In theory, this could break an existing setup. In the unlikely case that it actually does, you can fix it by setting the ignore-option to “[]” (square brackets without the quotes).

关于jquery - 对隐藏字段执行验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10136342/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com