如何在requirejs中使用jquery.validate?

时间:2016-04-18 20:18:18

标签: javascript jquery jquery-validate

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");  
});  

警报方法不起作用 但是,如果我删除

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

就像这样:

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

然后,警报方法有效 这对我来说,验证库和RequireJs一起存在问题 我确定我做的是一些微不足道的错误 我很感激你的帮助。感谢

2 个答案:

答案 0 :(得分:2)

它按预期工作。

WOrking fiddle - 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");  
});

另请参阅以下链接,该链接使用jquery验证和require js

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

答案 1 :(得分:0)

有类似的错误 只有我要使用本地化的消息 无法使其仅在本地使用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();
}); 

但是这个本地作品还可以

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();
});