Javascript 自动完成“不是一个功能”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5169009/
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
autocomplete "is not a function"
提问by Jacques
We've tested the Jquery UI (jquery-ui-1.8.10.custom.min.js) Autocomplete function in a simple HTML page which worked.
我们已经在一个简单的 HTML 页面中测试了 Jquery UI (jquery-ui-1.8.10.custom.min.js) 自动完成功能。
We then copy the same code into an Asp.net User Control and it stops working. The Javascript error reads "$searchBox.autocomplete is not a function".
然后我们将相同的代码复制到 Asp.net 用户控件中,然后它就停止工作了。Javascript 错误显示“$searchBox.autocomplete 不是函数”。
This user control is being used in an Asp.net Sitefinity 3.7 project. On the page it has a ScriptManager. Not sure what else I can add...
此用户控件正在 Asp.net Sitefinity 3.7 项目中使用。在页面上它有一个 ScriptManager。不知道我还能添加什么...
Anyone know what's going on?
有谁知道这是怎么回事?
Ammend:
修正:
<script src="/js/jquery-1.5.min.js" type="text/javascript"></script>
<script src="/js/jquery-ui-1.8.10.custom.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
var termTemplate = "<span class='ui-autocomplete-term'>%s</span>";
$('input#searchInput').autocomplete({
source: ['johannesburg z', 'johannesburg x', 'johannesburg v', 'johannesburg b', 'johannesburg a', 'johannesburg q', 'johannesburg u', 'johannesburg y', 'johannesburg o', 'johannesburg p'],
minLength: 3,
open: function (e, ui) {
var
acData = $(this).data('autocomplete'),
styledTerm = termTemplate.replace('%s', acData.term);
acData
.menu
.element
.find('a')
.each(function () {
var me = $(this);
me.html(me.text().replace(acData.term, styledTerm));
});
}
});
});
</script>
<div class="outerSearchBox">
<div class="searchFieldWrapper">
<input id="searchInput" type="text" class="searchField" /><a class="searchButton">SEARCH
</a>
<div class="searchSugContainer">
Thanks.
谢谢。
采纳答案by Phil
That error usually means that jquery or the plugin hasn't yet been loaded. Check that you're function call isn't getting hit before the document is loaded:
该错误通常意味着尚未加载 jquery 或插件。在加载文档之前检查您的函数调用是否未命中:
$(function(){
var $searchBox = $('#mysearchBox');
$searchBox.autocomplete(...);
});
Also check that the path to the javascript files are correct. Firebugor google chrome developer tools are useful for checking both of these issues.
还要检查 javascript 文件的路径是否正确。Firebug或 google chrome 开发人员工具可用于检查这两个问题。
回答by Ramnath
It could be:
它可能是:
- The order that jquery.js get loaded.
- Duplicate jquery.js includes in the same page.
- jquery.js 加载的顺序。
- 重复的 jquery.js 包含在同一页面中。
回答by Patrik Affentranger
Maybe try using the jQuery.noConflict() methodfor a test. It basically just makes you use a different alias for jQuery. This worked for me in SF4.3.
也许尝试使用jQuery.noConflict() 方法进行测试。它基本上只是让您为 jQuery 使用不同的别名。这在 SF4.3 中对我有用。
var j = jQuery.noConflict();
// Do something with jQuery
j("div p").hide();
// Do something with another library's $()
$("content").style.display = 'none';
回答by Jermel
I had this issue and what was happening was my jQuery file was loading twice. Pretty much this was in my head:
我遇到了这个问题,发生的事情是我的 jQuery 文件加载了两次。我脑子里大概是这样:
<script src="assets/js/jquery.min.js"></script>
<script src="js_css/jquery-ui.min.js" ></script>
And then I had an AJAX call that loads more parts of my document and there was another:
然后我有一个 AJAX 调用,它加载了我文档的更多部分,还有另一个:
<script src="assets/js/jquery.min.js"></script>
script tag in my AJAX results and I was $.hmtl() my results to my document. By doing this we are pretty much redefining all of the var thatjquery-ui.min.js altered or took advantage of.
我的 AJAX 结果中的 script 标签,我是 $.hmtl() 我的结果到我的文档。通过这样做,我们几乎重新定义了 jquery-ui.min.js 改变或利用的所有变量。
回答by Al Harding
This is caused by a conflict with Sitefinity's own use of jQuery.
这是由于与 Sitefinity 自己使用 jQuery 的冲突造成的。
If you move your script references for jQuery to the bottom of the page before the closing form tag. This will resolve the issue in Sitefinity 4.0, but I suspect it will also fix this problem in Sitefinity 3.7.
如果您将 jQuery 的脚本引用移动到页面底部的关闭表单标记之前。这将解决 Sitefinity 4.0 中的问题,但我怀疑它也将解决 Sitefinity 3.7 中的这个问题。