javascript jQuery 验证忽略:“:隐藏”并不总是有效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22632692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery Validation ignore: ":hidden" not always working
提问by LJ Wadowski
I have function to validation some special forms but I'm not able to exclude hidden elements
我有验证一些特殊表格的功能,但我无法排除隐藏的元素
function specialFormValidation() {
$("#specialForm").validate({
ignore: ':hidden',
errorPlacement: function(sError, oElement) {
$('.tooltip ').remove();
$(oElement).tooltip({
title: sError[0].textContent + $(oElement).is(':hidden'),
animation: false,
placement: 'right',
trigger: 'manual',
container: 'body'
});
$('.error').tooltip('show');
}
});
Like you can see I have set ignore parameter but is still showing tooltip for hidden elements, I'm using jQuery Validatre v1.10.0 and bootstrap tooltip. I can exclude visible elements if I put ignore: ':visible',
but its not working for hidden elements.
如您所见,我已设置忽略参数,但仍显示隐藏元素的工具提示,我使用的是 jQuery Validatre v1.10.0 和引导工具提示。如果我放置,我可以排除可见元素,ignore: ':visible',
但它不适用于隐藏元素。
Probably the ignore element not working on elements which was hidden by script after DOM was loaded
可能忽略元素对加载 DOM 后脚本隐藏的元素不起作用
I try to fix it like that (to refresh visibility status) but is also not working:
我尝试像这样修复它(以刷新可见性状态)但也不起作用:
function specialFormValidation() {
var ignored = $(':hidden');
$("#specialForm").validate({
ignore: ignored,
errorPlacement: function(sError, oElement) {
...
回答by Sparky
The ignore
option is for telling the plugin which input elements to ignore (for validation). I'm not sure why you need to worry about your tooltips since these are not input elements, and they would never be targeted for validation by this plugin in the first place.
该ignore
选项用于告诉插件忽略哪些输入元素(用于验证)。我不确定为什么你需要担心你的工具提示,因为它们不是输入元素,而且它们永远不会成为这个插件验证的目标。
Ignoring all hidden elements is the default behavior. In fact ':hidden'
is the same as what the plugin is already using.
忽略所有隐藏元素是默认行为。实际上':hidden'
与插件已经使用的相同。
In other words, as long as you're using version 1.9+ of the plugin, you don't need to specify ignore: ':hidden'
at all. Just leave out the ignore
option entirely and the plugin will ignore anything that's hidden.
换句话说,只要您使用的是 1.9+ 版本的插件,您就完全不需要指定ignore: ':hidden'
。 只需ignore
完全省略该选项,插件将忽略任何隐藏的内容。
Quote OP:
报价:
"Probably the ignore element not working on elements which was hidden by script after DOM was loaded"
“可能是忽略元素对加载 DOM 后脚本隐藏的元素不起作用”
This is not how it works. If you dynamically toggle the visibility of an element, the plugin will only ignore it while it's hidden.
这不是它的工作原理。如果您动态切换元素的可见性,插件只会在它隐藏时忽略它。
"I try to fix it like that (to refresh visibility status) but is also not working:"
“我尝试像这样修复它(以刷新可见性状态)但也不起作用:”
You cannot call .validate()
multiple times (with or without new options). The .validate()
method is only called onceon page load to initializethe plugin. Any subsequent calls are always ignored.
您不能.validate()
多次调用(有或没有新选项)。该.validate()
方法仅在页面加载时调用一次以初始化插件。任何后续调用始终被忽略。
回答by stephane bruno
You have to call "ignore" on document ready before you actually validate your form
在实际验证表单之前,您必须在准备好的文档上调用“忽略”
$(document).ready(function () {
$.validator.setDefaults({
ignore: []
});
});