jquery验证错误消息不隐藏,为什么(Jquery validation error messages are not hiding, why)
我正在使用jquery验证和jquery unobtrusive验证插件。 我试图隐藏错误消息,但错误消息没有隐藏。 我创建了一个js小提琴:
以下是我在小提琴中使用的代码:
HTML
<form id="form"> Name: <input data-val="true" data-val-length="The field First Name must be a string with a minimum length of 3 and a maximum length of 15." data-val-length-max="15" data-val-length-min="3" name="FirstName" type="text" /> <span class="field-validation-valid help-block" data-valmsg-for="FirstName" data-valmsg-replace="true"></span> </form> <button id="hide">Hide</button>
隐藏
JS
$("#hide").click(function(){ $("#form").data("validator").hideErrors(); });
我在这里失踪,为什么hideErrors()函数不工作?
编辑
我不知道为什么你们在回答问题的时候并不在意jquery不显眼的验证。 几乎所有的答案都是关注如何隐藏错误消息,而不用担心现有的功能,如果我们只隐藏消息就可能会中断。 我已经提到过我想要的答案
为什么jQuery验证器对象的hideErrors()函数不工作?
我还创建了一个最简单的小提琴演示我的问题, 请检查此问题。
I am using jquery validation and jquery unobtrusive validation plugins. I am trying to hide error message, but error message are not hiding. I have created a js fiddle:
Following is the code which i used in the fiddle:
Html
<form id="form"> Name: <input data-val="true" data-val-length="The field First Name must be a string with a minimum length of 3 and a maximum length of 15." data-val-length-max="15" data-val-length-min="3" name="FirstName" type="text" /> <span class="field-validation-valid help-block" data-valmsg-for="FirstName" data-valmsg-replace="true"></span> </form> <button id="hide">Hide</button>
Hide
JS
$("#hide").click(function(){ $("#form").data("validator").hideErrors(); });
What i am missing here and why hideErrors() function are not working ??
Edit
I don't know why you guys are not keeping jquery unobtrusive validation in mind while giving answers. Almost all answer are just focusing on how to hide error messages without worrying about the existing functionality which could be break if we just hide the message. I already mention that i want the answer of
Why hideErrors() function of jquery validator object is not working ?
I have also created a simplest possible fiddle which demonstrate my problem, check this.
原文:https://stackoverflow.com/questions/21985027
最满意答案
让我们用导入和生成器表达式清理其中一些函数,不是吗?
import string def clean_up(s): # I'm assuming you REQUIRE this function as per your assignment # otherwise, just substitute str.strip(string.punctuation) anywhere # you'd otherwise call clean_up(str) return s.strip(string.punctuation) def average_word_length(text): total_length = sum(len(clean_up(word)) for sentence in text for word in sentence.split()) num_words = sum(len(sentence.split()) for sentence in text) return total_length/num_words
您可能会注意到这实际上是浓缩为一个长度和不可读的单行:
average = sum(len(word.strip(string.punctuation)) for sentence in text for word in sentence.split()) / sum(len(sentence.split()) for sentence in text)
这是粗暴和恶心的,这就是为什么你不应该这样做;)。 可读性和所有这一切。
Let's clean up some of these functions with imports and generator expressions, shall we?
import string def clean_up(s): # I'm assuming you REQUIRE this function as per your assignment # otherwise, just substitute str.strip(string.punctuation) anywhere # you'd otherwise call clean_up(str) return s.strip(string.punctuation) def average_word_length(text): total_length = sum(len(clean_up(word)) for sentence in text for word in sentence.split()) num_words = sum(len(sentence.split()) for sentence in text) return total_length/num_words
You may notice this actually condenses to a length and unreadable one-liner:
average = sum(len(word.strip(string.punctuation)) for sentence in text for word in sentence.split()) / sum(len(sentence.split()) for sentence in text)
It's gross and disgusting, which is why you shouldn't do it ;). Readability counts and all that.
相关问答
更多-
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
两者都可以使用列表理解来完成: 1)最多20个字符: new_list = [item[:20] for item in wordlist] >>> new_list ["hi what's up home di", 'Oh wise master kakar', 'hello have a da'] 2)最多3个字: new_list = [' '.join(item.split()[:3]) for item in wordlist if item] >>> new_list ["hi what's ...
-
找到文本字符串中另一个单词的前一个单词的平均长度(Find the average length of the preceding word of another word in a string of text)[2022-09-28]
该解决方案使用字典,其值是所有在前单词的长度的列表。 给定的示例打印解决方案(最后一行)。 如果你不熟悉defaultdict请看这里 。 from collections import defaultdict def prevword_ave_len(word, text): words = defaultdict(list) #initialization of default dictionary textlist = text.split() #split t ... -
让我们用导入和生成器表达式清理其中一些函数,不是吗? import string def clean_up(s): # I'm assuming you REQUIRE this function as per your assignment # otherwise, just substitute str.strip(string.punctuation) anywhere # you'd otherwise call clean_up(str) return s.str ...
-
你的问题是你正在计算文本中的字符数,但是你必须计算每个字中的字符数。 最后,你甚至可以简化你的代码: def char(s): return [[word ,len(word)] for word in s.split()] 然后你可以通过以下方式调用它 text = "How are you today" l = char(text) print(l) 输出继电器: [['How', 3], ['are', 3], ['you', 3], ['today', 5]] Your problem ...
-
您将整个列表迭代一次,然后再次使用它迭代它,再次使用每个index调用进行迭代。 这不仅仅是浪费性能,它也过于复杂,更容易出错。 事实上,几乎每次你认为你想要list.index ,你实际上都没有,因为你将遇到重复值的问题 - 正是你遇到的问题。 如果你要求elephant的e索引,列表就无法知道你是否想要2而不是0 。 知道这一点的唯一方法是跟踪索引 - 例如,使用enumerate函数。 因此,更好的解决方案是循环一次: for i, letter in enumerate(chosen_word): ...
-
对列表进行排序,然后遍历它,将每个单词与随后的单词进行比较。 比较一个例程,告诉你在找到差异之前必须检查多少个字符。 随时跟踪最大“深度”。 编辑 - 根据主要字符告诉您两个词的“相似性”的功能: function similarity(w1, w2) { var i, l = Math.min(w1.length, w2.length); for (i = 0; i < l; ++i) if (w1[i] !== w2[i]) break; return i; } Sort th ...
-
问题是jquery验证插件无法读取单个标签的输入,它只是查看字符总数。 maxlength方法仅查看字段中的字符总数。 它没有做任何其他事情。 引用文档 : “使元素需要给定的最大长度” 如果要根据分隔符计算每个单词中的字符数,那么您需要为jQuery Validate编写自定义规则。 使用addMethod()方法创建自定义规则。 例: jQuery.validator.addMethod("taglength", function(value, element, param) { // your ...
-
尝试这个: import re def avrg_count(x): total_chars = len(re.sub(r'[^a-zA-Z0-9]', '', x)) num_words = len(re.sub(r'[^a-zA-Z0-9 ]', '', x).split()) print "Characters:{0}\nWords:{1}\nAverage word length: {2}".format(total_chars, num_words, total_char ...
-
只需在while循环外移动average计算和打印: while (in.hasNext()) { String word = in.next(); if (word.equals("exit")) { break; } double totalchar = word.length(); sum = totalchar + sum; counter++; } double average = 0; if (counter > 0) { ...