简体   繁体   English

如何在requirejs中使用jquery.validate?

[英]How to use jquery.validate with requirejs?

require.config({  
    paths:{  
        jquery:"lib/jquery-1.12.3.min",  
        bootstrap:"lib/bootstrap.min",  
        validate:"lib/jquery.validate.min"  
    },  
    shim:{  
        bootstrap:['jquery'],  
        validate:['jquery']    
    }  
});      


require(['jquery','validate'],function(){  
    alert("hope it works");  
});  

The alert method can not work. 警报方法无法正常工作。
However, if I remove 'validate' in 但是,如果我删除'验证'

require(['jquery','validate'],function(){  
     alert("hope it works");  
});  

just like this: 像这样:

require(['jquery'],function(){  
     alert("hope it works");  
});  

Then, the alert method works. 然后,警报方法工作。
This suggests to me there's an issue with the validate library and RequireJs together. 这告诉我,验证库和RequireJs存在问题。
I'm sure I'm doing something trivial wrong. 我确定我做的是一些微不足道的错误。
I'd appreciate the help. 我很感激你的帮助。 Thanks 谢谢

it is working as expected. 它按预期工作。

WOrking fiddle - https://jsfiddle.net/p15nn7jb/ 小提琴 - https://jsfiddle.net/p15nn7jb/

require.config({  
    paths:{  
        jquery:"https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery",  
        bootstrap:"https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/js/bootstrap.min",  
        validate:"https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.15.0/jquery.validate.min"  
    },  
    shim:{  
        bootstrap:['jquery'],  
        validate:['jquery']    
    }  
});      


require(['jquery','validate'],function(){  
    alert("hope it works");  
});

Also refer to the below link which is using jquery validation with require js 另请参阅以下使用jsery验证和require js的链接

https://jqueryvalidation.org/files/demo/requirejs/index.html https://jqueryvalidation.org/files/demo/requirejs/index.html

have a similar bug only i want to use the localized messages cant make it work with cdn only local 有一个类似的错误只有我想使用本地化的消息,不能使它与cdn只使用本地

    require.config({  
paths:{  
    jquery:"https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery",    
    validate:"https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.15.0/jquery.validate.min",  
    messages_he:"https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.18.0/localization/messages_he.min"  
},  
shim:{  
    bootstrap:['jquery'],  
    validate:['jquery']    
    }  
});      


require(['jquery','validate'],function(){  
    $.validator.setDefaults({
        submitHandler: function () {
            console.log("submitted!");
        }
    });
    $("form").validate();
}); 

but this local works ok 但这个本地工作正常

require.config({  
paths:{  
    jquery:"https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery",    
    validate:"libs/jquery-validate/1.15.0/jquery.validate.min",  
    messages_he:"libs/jquery-validate/1.18.0/localization/messages_he.min"  
},  
shim:{  
    bootstrap:['jquery'],  
    validate:['jquery']    
    }  
});      

require(['jquery', paths.jqueryval, paths.messages_he],function(){  
    $.validator.setDefaults({
        submitHandler: function () {
            console.log("submitted!");
        }
    });
    $("form").validate();
}); 

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

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