Javascript 未捕获的类型错误:无法读取未定义的属性“addMethod”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27211228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 23:52:15  来源:igfitidea点击:

Uncaught TypeError: Cannot read property 'addMethod' of undefined

javascriptjqueryjquery-validate

提问by Tabriz Atayi

jQuery(document).ready(function () {
    //alert("HIQ");
    $('.mySelectCalendar').datepicker({ firstDay: 1, dateFormat: "dd.mm.yy" });
    $.validator.addMethod(
            'date',
            function (value, element, params) {
                if (this.optional(element)) {
                    return true;
                };
                var result = false;
                try {
                    $.datepicker.parseDate('dd.mm.yy', value);
                    result = true;
                } catch (err) {
                    result = false;
                }
                return result;
            },
            ''
        );
});

I get error as "Uncaught TypeError: Cannot read property 'addMethod' of undefined"

我收到错误消息“未捕获的类型错误:无法读取未定义的属性‘addMethod’”

_layout as this

_layout 这样

@ViewBag.Title

@ViewBag.Title

<!-- jQuery -->
<script src="~/App_Themes/ThemeBlue/assets/js/jquery203.js"></script>
<script src="~/App_Themes/ThemeBlue/assets/js/jquery.min.js"></script>

<script type="text/javascript">
    var jQuery_2_0_3 = $.noConflict(true);
</script>
<!-- Picker UI-->
<script src="~/App_Themes/ThemeBlue/assets/js/jquery-ui.js"></script>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.1/jquery-ui.js"></script>


<!--Validation  -->
<script src="~/Scripts/jquery-1.7.1.min.js"></script>
<script src="~/Scripts/jquery.validate.js"></script> 
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>

<link href="~/App_Themes/ThemeBlue/css/validation.css" rel="stylesheet" />
<script type="text/javascript">
    var jQuery_1_7_0 = $.noConflict(true);
</script>
<!-- Bootstrap -->
<link href="~/App_Themes/ThemeBlue/dist/css/bootstrap.css" rel="stylesheet" media="screen" />
<link href="~/App_Themes/ThemeBlue/assets/css/custom.css" rel="stylesheet" media="screen" />


<!-- bin/jquery.slider.min.js -->


<script type="text/javascript" src="~/App_Themes/ThemeBlue/plugins/jslider/js/jshashtable-2.1_src.js"></script>
<script type="text/javascript" src="~/App_Themes/ThemeBlue/plugins/jslider/js/jquery.numberformatter-1.2.3.js"></script>
<script type="text/javascript" src="~/App_Themes/ThemeBlue/plugins/jslider/js/tmpl.js"></script>
<script type="text/javascript" src="~/App_Themes/ThemeBlue/plugins/jslider/js/jquery.dependClass-0.1.js"></script>
<script type="text/javascript" src="~/App_Themes/ThemeBlue/plugins/jslider/js/draggable-0.1.js"></script>
<script type="text/javascript" src="~/App_Themes/ThemeBlue/plugins/jslider/js/jquery.slider.js"></script>
<!-- Javascript  -->
<script src="~/App_Themes/ThemeBlue/assets/js/initialize-loginpage.js"></script>
<script src="~/App_Themes/ThemeBlue/assets/js/jquery.easing.js"></script>
<script src="~/App_Themes/ThemeBlue/assets/js/customTravel.js"></script>
<!-- Load Animo -->
<script src="~/App_Themes/ThemeBlue/plugins/animo/animo.js"></script>
<script src="~/App_Themes/ThemeBlue/dist/js/bootstrap.min.js"></script>

<!-- Carousel -->
<link href="~/App_Themes/ThemeBlue/examples/carousel/carousel.css" rel="stylesheet" />
<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
      <script src="assets/js/html5shiv.js"></script>
      <script src="assets/js/respond.min.js"></script>
    <![endif]-->

<!-- Fonts -->
<link href='http://fonts.googleapis.com/css?family=Lato:400,100,100italic,300,300italic,400italic,700,700italic,900,900italic' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Open+Sans:700,400,300,300italic' rel='stylesheet' type='text/css'>
<!-- Font-Awesome -->
<link rel="stylesheet" type="text/css" href="~/App_Themes/ThemeBlue/assets/css/font-awesome.css" media="screen" />
<!--[if lt IE 7]><link rel="stylesheet" type="text/css" href="~/App_Themes/ThemeBlue/assets/css/font-awesome-ie7.css" media="screen" /><![endif]-->

<!-- REVOLUTION BANNER CSS SETTINGS -->
<link rel="stylesheet" type="text/css" href="~/App_Themes/ThemeBlue/css/fullwidth.css" media="screen" />
<link rel="stylesheet" type="text/css" href="~/App_Themes/ThemeBlue/rs-plugin/css/settings2.css" media="screen" />

<!-- Picker UI-->
<link rel="stylesheet" href="~/App_Themes/ThemeBlue/assets/css/jquery-ui.css" />

<!-- bin/jquery.slider.min.css -->
<link rel="stylesheet" href="~/App_Themes/ThemeBlue/plugins/jslider/css/jslider.css" type="text/css">
<link rel="stylesheet" href="~/App_Themes/ThemeBlue/plugins/jslider/css/jslider.round.css" type="text/css">



<!-- Animo css-->
<link href="~/App_Themes/ThemeBlue/plugins/animo/animate_animo.css" rel="stylesheet" media="screen">
<!-- end -->

I write web application in MVC and could not solve this problem.

我在 MVC 中编写了 Web 应用程序,但无法解决这个问题。

COuld you help me?

你可以帮帮我吗?

回答by Sparky

Your error:

你的错误:

Uncaught TypeError: Cannot read property 'addMethod' of undefined

未捕获的类型错误:无法读取未定义的属性“addMethod”

It simply means that JavaScript cannot find the addMethodmethod, which is built into the jQuery Validate plugin. jQuery or jQuery Validate was not included properly... the file(s) cannot be found or jQuery is broken...

这只是意味着 JavaScript 无法找到addMethod内置于 jQuery Validate 插件中的方法。jQuery 或 jQuery Validate 未正确包含...无法找到文件或 jQuery 已损坏...

Your head section is a mess with multiple versions of jQuery and .noConflict()applied inconsistently.

您的 head 部分与多个版本的 jQuery 杂乱无章,并且.noConflict()应用不一致。

Notice how you've define .noConflict()just after this particular version of jQuery is included?

请注意.noConflict()在包含这个特定版本的 jQuery 之后您是如何定义的?

<!--Validation  -->
<script src="~/Scripts/jquery-1.7.1.min.js"></script>
<script src="~/Scripts/jquery.validate.js"></script> 
....
<script type="text/javascript">
    var jQuery_1_7_0 = $.noConflict(true);  // <- this
</script>

So for the validation section, the name jQuery_1_7_0must replace every instance $just within your validation code.

因此,对于验证部分,名称jQuery_1_7_0必须替换$验证代码中的每个实例。

jQuery_1_7_0(document).ready(function () {

    jQuery_1_7_0.validator.addMethod( ....

I've only pointed out one example. You've also included a version of jQuery without .noConflict()just above your validation section.

我只举了一个例子。您还在.noConflict()验证部分上方包含了一个 jQuery 版本。

These multiple versions of jQuery need to be resolved, either by removing the duplicates and leaving one version or by properly using .noConflict()after each version is included.

这些多个版本的 jQuery 需要解决,要么删除重复项并保留一个版本,要么.noConflict()在包含每个版本后正确使用。

IMO, it's best to only use one version of jQuery.

IMO,最好只使用一个版本的 jQuery。

Documentation: Using jQuery .noConflict()

文档使用 jQuery.noConflict()

回答by gauravbhai daxini

Add jquery.validate.jsfile and jquery.min.jsif you have not included and then write your code of js for add 'addMethod' method.

添加jquery.validate.js文件,jquery.min.js如果您还没有包含,然后编写您的 js 代码以添加“addMethod”方法。