jquery validate not loaded requirejs

烂漫一生 提交于 2019-12-13 16:26:24

问题


I have a main module in RequireJS:

require([
    'jquery',
    'jquery.validate',
    'jquery.validate.unobtrusive'
], function ($) {
    $(document).ready(function () {
        var validator = $("form").validate();
        if ($("#txtFirstName").val() !== "")
            validator.element("#txtFirstName");
    });
});

When I load this page, I get a JavaScript error:

TypeError: $(...).validate is not a function var validator = $("form").validate();**

I don't now why? All scripts are loaded:


回答1:


You'll need to configure shim to "wire" the dependencies correctly:

require.config({
  paths: {
    'jquery': 'path-to-jquery',
    'jquery.validate': 'path-to-jquery-validate',
    'jquery.validate.unobtrusive': 'path-to-jquery-validate-unobtrusive'
  },
  shim: {
    'jquery.validate': ['jquery'],
    'jquery.validate.unobtrusive': ['jquery', 'jquery.validate']
  }
});

require(['jquery', 'jquery.validate', 'jquery.validate.unobtrusive'], function ($) {
  // your code
});

More details (and examples) in the official documentation (look for the "For "modules" that are just jQuery or Backbone plugins..." section).



来源:https://stackoverflow.com/questions/18955018/jquery-validate-not-loaded-requirejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!