Javascript 另一个“对象不支持此属性或方法” - jQuery

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4176656/
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-23 11:15:38  来源:igfitidea点击:

Yet Another "Object doesn't support this property or method" - jQuery

javascriptjquery

提问by Chase Florell

I just switched my App to run on MVC3 and the Razor view engine, and now I'm getting a JavaScript error. The thing is, nothing has changed on the JavaScript side of things... it worked before.

我刚刚将我的应用程序切换为在 MVC3 和 Razor 视图引擎上运行,现在我收到了 JavaScript 错误。问题是,JavaScript 方面没有任何改变……它以前有效。

Here's the code

这是代码

<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
    var json_region = [{"value":365,"label":"Calgary"},{"value":368,"label":"Canmore"},{"value":393,"label":"Edmonton"}]
    $(function() {
        $('#UserRegion').autocomplete({
            source: json_region,
            selectFirst: true,
            select: function( event, ui ) {
                $('#RegionID').val( ui.item.value );
                $('#UserRegion').val( ui.item.label );
                return false;
            }
        });
    });
</script>

<script type="text/javascript" src="/Extras/urbannow.js/1"></script>
<script src="/Assets/Scripts/jquery.ui.autocomplete.selectfirst.js" type="text/javascript"></script>
<script src="/Assets/Scripts/wmd.js" type="text/javascript"></script>
<script src="/Assets/Scripts/showdown.js" type="text/javascript"></script>
<script src="http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.2.min.js"></script>
<script src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js"></script>
<script src="/Assets/Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>

And this is erroring

这是错误的

$('#UserRegion').autocomplete({

The console says

控制台说

SCRIPT438: Object doesn't support this property or method

SCRIPT438: 对象不支持此属性或方法

And I just can't figure this one out.

我就是想不通这个。

回答by RPM1984

You're including jQuery twice.

您将 jQuery 包含两次。

回答by Hamish

You're calling .autocompleteimmediately after including the base jQuery library - which does not include the autocomplete plugin. Fix the order of your script references and make sure the autocomplete plugin is included before you try to use it.

.autocomplete在包含基本 jQuery 库后立即调用- 不包含自动完成插件。修复脚本引用的顺序,并确保在尝试使用之前包含自动完成插件。

回答by Elijan Sejic

load this BEFORE you custom script call

在您自定义脚本调用之前加载这个

<script src="/Assets/Scripts/jquery.ui.autocomplete.selectfirst.js" type="text/javascript"></script>

<script src="/Assets/Scripts/jquery.ui.autocomplete.selectfirst.js" type="text/javascript"></script>

or at very best custom code should be after all your JavaScript files. so , your code should look like something like this

或者最好的情况是自定义代码应该放在所有 JavaScript 文件之后。所以,你的代码应该像这样

 <script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.min.js"></script>
    <script type="text/javascript" src="/Extras/urbannow.js/1"></script>
    <script src="/Assets/Scripts/jquery.ui.autocomplete.selectfirst.js" type="text/javascript"></script>
    <script src="/Assets/Scripts/wmd.js" type="text/javascript"></script>
    <script src="/Assets/Scripts/showdown.js" type="text/javascript"></script>
    <script src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js"></script>
    <script src="/Assets/Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>

  <script type="text/javascript">
    var json_region = [{"value":365,"label":"Calgary"},{"value":368,"label":"Canmore"},{"value":393,"label":"Edmonton"}];
    $(function() {
        $('#UserRegion').autocomplete({
            source: json_region,
            selectFirst: true,
            select: function( event, ui ) {
                $('#RegionID').val( ui.item.value );
                $('#UserRegion').val( ui.item.label );
                return false;
            }
        });
    });
</script>

回答by Alexey Semenko

Duplicate jQuery declaration, check View Source of page

重复 jQuery 声明,检查页面的查看源