jquery验证未加载requirejs [英] jquery validate not loaded requirejs

查看:77
本文介绍了jquery验证未加载requirejs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在RequireJS中有一个主要模块:

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

当我加载此页面时,我收到一个JavaScript错误:

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


TypeError:$(...)。validate不是函数
var validator = $(form)。validate(); **

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

我现在不知道为什么?所有脚本都已加载:

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

推荐答案

你需要配置 shim 来连接正确依赖:

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

更多详细信息(和示例)在官方文档中(查找For模块只是jQuery或Backbone插件......部分)。

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

这篇关于jquery验证未加载requirejs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆