使用 jquery.validationEngine.js 进行验证时如何忽略隐藏的页面元素?
我有一个 div,其样式默认为“display: none”,直到填写了前一个字段,并且它包含一个需要选择的下拉列表。
如果在此 div 可见之前提交页面,则验证错误将显示在页面的左半部分。
当然,这不是一个功能问题,但我一直想知道是否有办法让验证引擎忽略隐藏元素。
到目前为止,我已经尝试过 class="validate[optional: Special]" 正如 作者博客: "可选:特殊:仅在字段不为空时验证*请先调用可选"。它似乎没有按照建议工作。
<div id="container" style="display: none;">
...
<select id="mapLocation" onchange="moveMapToCenter();" class="validate[optional: Special]" />
...
</div>
我还尝试过使用 jquery.validate“ignore”:
$(document).ready(function() {
$("#aspnetForm").validationEngine({
ignore: ":hidden"
success: false,
failure: function() {}
})
});
这可能是我的一个简单的疏忽,我们拭目以待!谢谢。
I have a div who's style defaults to "display: none" until a previous field has been filled out, and it contains a drop down list that requires a selection.
If the page is submitted before this div becomes visible, the validation error appears to the left half-off the page.
This is not a functional problem of course, but I've been wondering if there's a way to have the validation engine ignore hidden elements.
So far I've tried class="validate[optional: Special]" as pointed out on the creator's blog: "optional: Special: Only validate when the field is not empty *Please call optional first". It doesn't seem to work as suggested.
<div id="container" style="display: none;">
...
<select id="mapLocation" onchange="moveMapToCenter();" class="validate[optional: Special]" />
...
</div>
I've also tried using jquery.validate "ignore":
$(document).ready(function() {
$("#aspnetForm").validationEngine({
ignore: ":hidden"
success: false,
failure: function() {}
})
});
This may be a simple oversight on my part, we'll see! Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我最终通过隐藏和显示元素的函数添加和删除validation[require]类属性来解决这个问题。
当隐藏需要验证的页面元素时,这可以很好地避免验证错误消息出现在奇怪的地方。
I ended up solving this by adding and removing the validation[required] class attribute through functions that hide and show the element.
This works fairly well to avoid validation error messages showing up in strange places when hiding page elements that require validation.
我需要禁用临时更多字段,我做了这个,一个很好的解决方案:
一个小的 jquery 扩展:
用于禁用字段验证:
重新启用字段验证的用法:
i need disable temporary more fields, i make this, a pretty solution:
a little jquery extension:
usage to disable a field validation:
usage to re-enable a field validation: