【问题标题】:jQuery validate error "jquery.format is not a function" after plugin update插件更新后 jQuery 验证错误“jquery.format 不是函数”
【发布时间】:2021-01-11 14:56:07
【问题描述】:

多年来,我成功使用 jQuery.validate 版本 1.9.0
现在,使用 v 1.19.2 我得到“jquery.format 不是函数”。
在我的头部:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.2/jquery.validate.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.2/additional-methods.min.js" ></script>    

html表单输入

<div class="divTableCell L3"><input name="email" type="text" class="required" id="email" /></div>

在底部:

<script>
    formVerif();
</script>

这是使用的函数的一部分:

function formVerif(){   
    $(document).ready(function(){
        $("#formulario").validate({
          errorClass: 'rojo' ,
          messages: {
               nombre: {
                 required: "<br />Falta ingresar un nombre.",
                 minlength: jQuery.format("<br />Ingresa al menos {0} caracteres"),
                 maxlength: jQuery.format("<br />Ingresa como máximo {0} caracteres"),
                 alphanumeric: "<br />Ingresa solo letras y espacios"
                 },
               apellido: {
               ...
               ...

如果我回到 V 1.9.0,则不会显示错误:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="JS/jquery.validate.min.js"></script>
<script src="JS/additional-methods.min.js"></script>

【问题讨论】:

  • 没有jQuery.format 这样的东西——应该是jQuery.validator.format。您的示例代码也不完整。您只向我们展示了带有 name="email" 的字段的 HTML,然后展示了带有完全不同字段的 .validate() 方法。

标签: jquery jquery-validate


【解决方案1】:

您的代码...

minlength: jQuery.format("<br />Ingresa al menos {0} caracteres"),

没有jQuery.format这样的东西,所以只需删除并使用它...

minlength: "<br />Ingresa al menos {0} caracteres",

它仍然可以正常工作......

演示jsfiddle.net/w09fqj3n/

Reviewing the documentationjQuery.format 应该是 jQuery.validator.format,但它显然是可选的。


此外,将 DOM 就绪事件处理程序放在您在页面底部调用的函数中没有意义...

function formVerif(){   
    $(document).ready(function(){ ....

the ready event handler 的主要目的是“指定一个在 DOM 完全加载时执行的函数。” 从页面底部的函数调用它会使它的使用完全没有意义。如果您希望将加载脚本延迟到最后,只需将就绪处理程序放在底部即可。

【讨论】:

    猜你喜欢
    • 2018-02-25
    • 1970-01-01
    • 1970-01-01
    • 2011-01-07
    • 2018-05-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-31
    • 1970-01-01
    相关资源
    最近更新 更多