首页 \ 问答 \ jquery验证错误消息不隐藏,为什么(Jquery validation error messages are not hiding, why)

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:

Fiddle Link

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
更新时间:2022-09-12 14:09

最满意答案

让我们用导入和生成器表达式清理其中一些函数,不是吗?

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.

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。