简体   繁体   English

this.init不是React App中的功能jquery验证插件

[英]this.init is not a function jquery validation plugin in react app

I have form in my react application, which validates via jquery validation plugin. 我的应用程序中有表单,该表单通过jquery验证插件进行验证。

let validator = $('form').validate({
        errorClass:  'has-error',
        errorElement:'label',
    });
    validator.form();
    if ($('.has-error').length > 0) {
        $('.has-error').each(function (index) {
            $.validator().showErrors({prop:$(this).data('error')});
        });
    } else {
         /*work with data*/
}

All errors messages showing fine, but every time when validation triggered, I get error in console: 所有错误消息均显示正常,但是每次触发验证时,控制台都会出现错误:

this.init is not a function

And link me to code in plugin script: 并将我链接到插件脚本中的代码:

$.validator = function( options, form ) {
    this.settings = $.extend( true, {}, $.validator.defaults, options );
    this.currentForm = form;
    this.init();
};

How can I fix it? 我该如何解决? UPD 1: below in the plugin script code i found this code: UPD 1:在插件脚本代码下面,我找到了此代码:

$.extend( $.validator, {
//some code
prototype: {

        init: function() {
            this.labelContainer = $( this.settings.errorLabelContainer );
            this.errorContext = this.labelContainer.length && this.labelContainer || $( this.currentForm );
            this.containers = $( this.settings.errorContainer ).add( this.settings.errorLabelContainer );
            this.submitted = {};
            this.valueCache = {};
            this.pendingRequest = 0;
            this.pending = {};
            this.invalid = {};
            this.reset();
//some code

Maybe it fires error exception because of this init function? 也许由于这个init函数而引发错误异常?

I think the problem is in this line: 我认为问题出在这一行:

$.validator().showErrors({prop:$(this).data('error')});

$.validator function is a constructor so it must always be used with new keyword. $.validator函数是一个构造函数,因此必须始终与new关键字一起使用。 If you call it as normal function this inside this function points to global window (or is undefined in strict mode ) which doesn't have init method 如果您将其称为普通函数,则this函数内部将指向没有init方法的全局window (或在strict mode undefined

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

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