javascript 未捕获的类型错误:无法读取未定义的属性“构造函数”

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

Uncaught TypeError: Cannot read property 'Constructor' of undefined

javascriptjqueryasp.net-mvc-3jquery-uitypeahead.js

提问by NealR

I am getting this and a couple other weird error messages on my main ASP MVC 3 layout page.

我在我的主要 ASP MVC 3 布局页面上收到了这个和其他一些奇怪的错误消息。

I am using the following scripts

我正在使用以下脚本

<script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-1.8.3.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-ui.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/typeahead.js")" type="text/javascript"></script>

And trying to get the typeahead.jsto work in our nav bar. Below is the code I currently have on my machine. I had this working at one point in time, however after a sync up with a coworker the typeahead search does not work anymore and I haven't been able to track down the issue.

并试图让typeahead.js在我们的导航栏中工作。下面是我目前在我的机器上的代码。我曾在某个时间点进行过这项工作,但是在与同事同步后,预先输入的搜索不再起作用,而且我无法找到问题所在。

The first line in the code below gives the following error

下面代码中的第一行给出了以下错误

Uncaught TypeError: Cannot read property 'Constructor' of undefined

If I comment out the protoype.blurfunction, however, I then get this error

protoype.blur但是,如果我注释掉该函数,则会收到此错误

Uncaught TypeError: Object [object Object] has no method 'typeahead' 

Which points to the $('.AgentSearch .typeahead).typeaheadfunction. Been stuck on this for hours, any help would be greatly appreciated. Thanks

哪个指向$('.AgentSearch .typeahead).typeahead函数。被困在这几个小时,任何帮助将不胜感激。谢谢

        // Workaround for bug in mouse item selection
        $.fn.typeahead.Constructor.prototype.blur = function () {
            var that = this;
            setTimeout(function () { that.hide() }, 250);
        };

        $('.AgentSearch').typeahead({
            source: function (term, process) {
                var url = '@Url.Action("GetAgents", "Agent")';
                var agents = [];
                map = {};
                return ($.getJSON(url, { term: term }, function (data) {
                    $.each(data, function (i, item) {
                        map[item.Name] = item;
                        agents.push(item.Name);
                    });
                    process(agents);
                }));
            },
            highlighter: function (item) {
                var p = map[item];
                var display = ''
                             + "<div class='typeahead_wrapper'>"
                             + "<div class='typeahead_labels'>"
                             + "<div class='typeahead_primary'>" + p.Name + "</div>"
                             + "<div class='typeahead_third'><i>LastFour:</i> " + p.LastFour + "</div>"
                             + "</div>"
                             + "</div>";
                return display;
            },
            updater: function (item) {
                window.location.href = ("/Monet/Agent/Details/" + map[item].SymetraNumber);
            }
        });

Here is the search box I'm binding this to

这是我将其绑定到的搜索框

            <form class="navbar-search pull-left">   
                <input type="text" name="names" value="" id="AgentSearch" class="search-query" data-provide="typeahead" placeholder=" Agent Search"/> 
            </form>

EDIT

编辑

Here are the script tags as they're rendered by the browser

这是浏览器呈现的脚本标签

<script src="/Scripts/modernizr-1.7.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery-1.8.3.js" type="text/javascript"></script>
<script src="/Scripts/jquery-ui.js" type="text/javascript"></script>
<script src="/Scripts/typeahead.js" type="text/javascript"></script>

回答by theUtherSide

It looks like typeahead is not defined.

看起来没有定义预先输入。

If you're trying to extend typeahead, this article may help: Extend the bootstrap-typeahead in order to take an object instead of a string

如果您尝试扩展 typeahead,这篇文章可能会有所帮助: Extend the bootstrap-typeahead in order to take an object 而不是 string

$.fn will always return an object ('[]'), this is why the error change when you comment-out .prototype.blur, but they're both indicating 'typeahead' doesn't exist.

$.fn 将始终返回一个对象 ('[]'),这就是为什么当您注释掉 .prototype.blur 时错误会发生变化,但它们都表明 'typeahead' 不存在。

Just a guess, but it could be a synchronicity issue where your code is running before the library loads.

只是一个猜测,但它可能是一个同步问题,你的代码在库加载之前运行。