简体   繁体   English

在jquery-ui自动完成中处理来自自定义源的数据

[英]Handling data from a custom source in jquery-ui autocomplete

I am trying to use jquery-ui for autocompletion in a search field. 我试图在搜索字段中使用jquery-ui进行自动完成。 Because the search depends on the value of another form field, I'm using a callback for the source. 因为搜索取决于另一个表单字段的值,所以我正在使用源代码的回调。 I can see that the request is sent correctly. 我可以看到请求已正确发送。 My remote script returns a simple array of strings and it's at that point that I can't get it to work. 我的远程脚本返回一个简单的字符串数组,此时我无法使其工作。 The dropdown list is never built. 永远不会构建下拉列表。 Can anyone tell me why? 谁能告诉我为什么? Here's the code: 这是代码:

    $(document).ready(function(){
    $("#species").autocomplete({
      source: function( request, response ) {
        $.ajax({
          url: "/includes/species-ajax.cfm",
          dataType: "jsonp",
          data: {
            term: request.term,
            searchBy : function() { 
              var sb = $("#searchBy_hidden").val();
              return (sb ? sb : 'common_name'); }
          },
          success: function( data ) {
            response( $.map( data, function( item ) {
              return {
                label: item.name,
                value: item.name
              }
            }));
          }
         });
    }});
  });

<input type="hidden" name="searchBy_hidden" id="searchBy_hidden" value="common_name" />
Enter the name of a species: <input type="textbox" size="30" id="species" />

Thanks, 谢谢,

尝试将dataType更改为'json' ,而不是'jsonp'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM