2014-01-22 177 views
1

我有这个js脚本:jQuery的自动完成功能不显示结果框

$('#other_teacher').autocomplete({ 
    source: function (request, response) { 
     $.ajax({ 
      url: '/app_dev.php/en/teacher/ajax/courseadd-teacher/'+request.term, 
      type: 'GET', 
      dataatType: 'json', 
      success: function(data) { 
       console.log(data); 
       response($.each(data, function(index, value) { 
        return { 
         label: value, 
         value: index 
        } 
       })); 
      } 
     }); 
    }, 
    minLength: 1 
}); 

和Ajax请求的结果是:

[{"id":2,"name":"Moran bob"},{"id":2,"name":"Willam Lawsan"}] 

阿贾克斯resquest开始,数据有结果的对象,但结果窗口不显示。

+0

你能张贴您的HTML也请 – dcodesmith

+0

把这个CSS http://code.jquery.com/ui/ 1.10.3/themes/smoothness/jquery-ui.css并尝试使用我们的代码或我的 – Dinesh

+0

有什么好运?这是一个很好的做法,让我们知道你的进步 – dcodesmith

回答

0

为自动完成放CSS和那就试试这个代码

$('#textbox id').autocomplete({ 
     source: function (request, response) { 
      $.getJSON("url?term=" + request.term, function (data) { 
       response(data); 

      }); 

     }, 
     minLength: 1, 
     delay: 100 

    }); 
1

使用$.map而不是$.each,然后访问从对象的字段的值。 我还增加了一个选择功能和记录器内进行确认检查,如果你实际上得到任何东西

$('#other_teacher').autocomplete({ 
    source: function (request, response) { 
     $.ajax({ 
      url: '/app_dev.php/en/teacher/ajax/courseadd-teacher/'+request.term, 
      type: 'GET', 
      dataatType: 'json', 
      success: function(data) { 
       console.log(data); 
       response($.map(data, function(item) { 
        return { 
         label: item.id, // or item.name if you want 
         value: item.name 
        } 
       })); 
      } 
     }); 
    }, 
    minLength: 1, 
    select: function (event, ui) { 
     console.log(ui.item); 
    } 
});