无法使用jQueryUI绑定自动完成

时间:2013-04-08 10:18:03

标签: jquery-ui jquery-autocomplete

我正在尝试使用jqueryUi进行自动完成,但无法将数据绑定到它。

$("#CustomerName").autocomplete({
        source:
       function () {
           $.ajax({
               url: "SearchCustomer?key=" + $("#CustomerName").val(),
               async: false,
               dataType:"json",
               success: function (data) {
                   return data.ResultList;
               }
           })
       },
        minLength: 0, autoFocus: true, delay: 1000
    });

我的ajax结果是 -

{"Message":null,"Successfull":false,"Id":0,"Result":null,"ResultList":["Customer 2","Kohl\u0027s Corp","Test Corp"]}

如果我使用它,那么它可以正常工作

$("#CustomerName").autocomplete({
            source:["Customer 2","Kohl\u0027s Corp","Test Corp"],
            minLength: 0, autoFocus: true, delay: 1000
        });

提前致谢!

1 个答案:

答案 0 :(得分:3)

$ .ajax没问题,你能否用response(data.ResultList);

这样的回复包装返回的数据
$("#CustomerName").autocomplete({
    source: function( request, response ) {
            $.ajax({
               url: "SearchCustomer?key=" + $("#CustomerName").val(),
               dataType : "json",
               success: function (data) {
                   response(data.ResultList);
               }
           });
    },
    minLength: 0, 
    autoFocus: true, 
    delay: 1000
});