如何在requirejs中使用jquery.validate?

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

然后,alert方法起作用了。
这让我想到validate库和RequireJs一起存在问题。
我确定我做错了一些微不足道的事情。
感谢您的帮助。谢谢


它按预期工作。工作的fiddle - https://jsfiddle.net/p15nn7jb/ - koolhuman
2个回答

4
我会尽力为您翻译中文。以下是需要翻译的内容:

它正在按预期工作。

工作的样例 - 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 validation和require js:

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


0

有一个类似的 bug,但我想使用本地化的消息,只有本地才能使它工作,无法使用 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();
}); 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接