我试图在我的应用程序中集成
Jquery自动完成功能.所需的js文件包括如下:
<script type="text/javascript" src="${pageContext.request.contextPath}/scripts/jquery-1.8.3.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/scripts/jquery.ui.core.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/scripts/jquery.ui.widget.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/scripts/jquery.ui.autocomplete.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/scripts/jquery-ui.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/scripts/script.js"></script>
‘scripts.js’文件是特定于应用程序的文件,包含如下代码:
$(document).ready(function() {
$('#aisleFrom').autocomplete({
url: '/StoreMapperApp/MapPickingZone.action?autocomplete=',
minChars: 0,
max: 10,
width: 150,
scroll: true,
cacheLength: 0
}).focus(function() {
$(this).autocomplete('search', $(this).val())
});
});
但是,当我尝试输入id为’aisleFrom’的文本字段时,我在jquery-ui.min.js文件中收到如下错误:
TypeError:this.source不是函数
…完全加载“),this.cancelSearch = 1,this.source!({项:电子},this._response())},…
有人可以建议吗?
最佳答案 尝试将网址更改为源.见
Autocomplete Widget source
jQuery(document).ready(function() {
$("#aisleFrom").autocomplete({
minLength: 0,
// substitute `source` for `url`
source: function(request, response) {
var term = request.term;
// get json
$.getJSON(/* /path/to/json/ */)
.then(function success(data) {
// filter results
var res = $.grep(data, function(val) {
return new RegExp($.ui.autocomplete.escapeRegex(term), "gi")
.test(val.toLowerCase())
})
, key = $.inArray(term.toUpperCase(), res)
, results = term.length === 1
& key !== -1
? Array(res[key])
: res;
response(results)
}, function error(jqxhr, textStatus, errorThrown) {
console.log(textStatus, errorThrown) // log `$.ajax` errors
})
}
}).focus(function() {
$(this).autocomplete("search", $(this).val())
});
});
jQuery(document).ready(function() {
$("#tags").autocomplete({
minLength: 0,
source: function(request, response) {
var term = request.term;
$.getJSON("https://gist.githubusercontent.com/anonymous/86f61fee217838ba6c3c/raw/395a557fa400163f048f30370d782db554913b2b/availableTags.json")
.then(function success(data) {
var res = $.grep(data, function(val) {
return new RegExp($.ui.autocomplete.escapeRegex(term), "gi")
.test(val.toLowerCase())
})
, key = $.inArray(term.toUpperCase(), res)
, results = term.length === 1
&& key !== -1
? Array(res[key])
: res;
response(results)
}, function error(jqxhr, textStatus, errorThrown) {
console.log(textStatus, errorThrown) // log `$.ajax` errors
})
}
}).focus(function() {
$(this).autocomplete("search", $(this).val())
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/jquery-ui-git.js"></script>
<div class="ui-widget">
<label for="tags">Tags:</label>
<input id="tags">
</div>