jQuery使用ajax自动完成问题-TypeError:this.source不是函数

jQuery autocomplete problems with ajax - TypeError: this.source is not a function

本文关键字:this source 函数 -TypeError 成问题 ajax 使用 jQuery      更新时间:2023-09-26

我使用的是jQueryUI自动完成函数,它可以很好地处理本地变量中的数据,但当使用$.get请求中的数据时,我会收到以下错误:TypeError:this.source不是函数。如果我在代码中删除$(function(){,则在自动完成中没有错误,但仍然没有数据。

Content in: index.html
<script>
$(function(){
var ajaxData;
$.get('ajaxdata.html', function(data) {
$('.result').html(data);
console.log('Load was performed.'+data);
ajaxData = data;
});
var localData = ['ActionScript','AppleScript','Scheme'];
$( "#tags" ).autocomplete({
//source: localData //working
source: ajaxData //not working
});
});
</script>
<input id="tags">
Content in: ajaxdata.html
['ActionScript','AppleScript','Scheme']

例如:

// use document ready
$(document).ready(function(){
  $.get('ajaxdata.html', function(data) {
    $('.result').html(data);
    console.log('Load was performed.'+data);
    $( "#tags" ).autocomplete({
      source: data
    });
});