简体   繁体   English

定义全局变量-jquery.validate不起作用

[英]Defining global variable in - jquery.validate not working

I am trying get the value of global variable in jquery validation plugin. 我正在尝试在jquery验证插件中获取全局变量的值。

var typedWords = 1;
jQuery.validator.addMethod("wordCount",
       function(value, element, params) {
                 typedWords = jQuery.trim(value).split(' ').length;
                 console.log(typedWords);
                 if (typedWords >= params[0] && typedWords <= params[1]) {
                     return true;
                  }
               },
               jQuery.format("Only {0} to {1} words allowed. You have entered " + typedWords + " words.")
            );

however the value of typedWords in jQuery.format("Only {0} to {1} words allowed. You have entered " + typedWords + " words.") is always undifined . 但是jQuery.format("Only {0} to {1} words allowed. You have entered " + typedWords + " words.")的typedWords的值jQuery.format("Only {0} to {1} words allowed. You have entered " + typedWords + " words.")始终是undifined

How am i suppose to get the value of typedWords = jQuery.trim(value).split(' ').length; 我应该如何获得typedWords = jQuery.trim(value).split(' ').length; . typedWords = jQuery.trim(value).split(' ').length; ?

Thanks. 谢谢。

Looking at the console errors, it's telling me that $.format from jquery.validate.js has been deprecated. 查看控制台错误,它告诉我$.format中的$.format jquery.validate.js已被弃用。 So simply change $.format into $.validator.format , or in your case... 所以,简单地改变$.format$.validator.format ,或在您的情况...

jQuery.validator.format("Only {0} to {1} words allowed. You have entered " + typedWords + " words.")

( Wrapping the custom message within $.validator.format() is no longer needed at all, as leaving it out entirely make no difference. ) 不再需要将自定义消息包装在$.validator.format()中,因为将其完全忽略不计。

It works now, except the typedWords variable will never be updated beyond the 1 you've defined globally. 它现在可以正常工作,除了typedWords变量永远不会在您全局定义的1之外更新。 That's because once you've initialized the custom method, the only pieces of the custom message that can dynamically change are the parameters at {0} and {1} . 这是因为初始化自定义方法后,唯一可以动态更改的自定义消息只有{0}{1}处的参数。

DEMO: http://jsfiddle.net/JRgN7/ 演示: http : //jsfiddle.net/JRgN7/

However, if you assign typedWords to another parameter called params[2] , you can use {2} within the message and it correctly updates itself. 但是,如果将typedWords分配给另一个名为params[2]参数,则可以在消息中使用{2} ,并且它会正确更新。

var typedWords = 1;
jQuery.validator.addMethod("wordCount", function (value, element, params) {
    typedWords = jQuery.trim(value).split(' ').length;
    params[2] = typedWords;
    console.log(typedWords);
    if (typedWords >= params[0] && typedWords <= params[1]) {
        return true;
    }
},
jQuery.validator.format("Only {0} to {1} words allowed. You have entered {2} words."));

DEMO: http://jsfiddle.net/JRgN7/1/ 演示: http : //jsfiddle.net/JRgN7/1/

Now we can just use params[2] and eliminate the global typedWords variable entirely. 现在我们可以使用params[2]并完全消除全局typedWords变量。 Also, it's not necessary to put the custom message inside jQuery.validator.format() anymore. 另外,也不必将自定义消息放在jQuery.validator.format()

jQuery.validator.addMethod("wordCount", function (value, element, params) {
    params[2] = jQuery.trim(value).split(' ').length;
    if (params[2] >= params[0] && params[2] <= params[1]) {
        return true;
    }
},
"Only {0} to {1} words allowed. You have entered {2} words.");

FINAL Working DEMO: http://jsfiddle.net/JRgN7/3/ 最终工作演示: http : //jsfiddle.net/JRgN7/3/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM