jQuery自动完成ajax的问题​​ - TypeError:this.source不是一个函数

时间:2012-09-09 21:57:26

标签: javascript jquery

我正在使用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']

1 个答案:

答案 0 :(得分:1)

例如:

// use document ready
$(document).ready(function(){
  $.get('ajaxdata.html', function(data) {
    $('.result').html(data);

    console.log('Load was performed.'+data);

    $( "#tags" ).autocomplete({
      source: data
    });
});