【问题标题】:jquery: TypeError: this.source is not a functionjquery: TypeError: this.source 不是一个函数
【发布时间】:2015-04-30 11:35:28
【问题描述】:

我正在尝试在我的应用程序中集成 Jquery 自动完成功能。所需的js文件如下:

<script type="text/javascript" src="${pageContext.request.contextPath}/scripts/jquery-1.8.3.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/scripts/jquery.ui.core.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/scripts/jquery.ui.widget.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/scripts/jquery.ui.autocomplete.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/scripts/jquery-ui.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/scripts/script.js"></script>

“scripts.js”文件是包含如下代码的应用程序特定文件:

$(document).ready(function() {
    $('#aisleFrom').autocomplete({
        url: '/StoreMapperApp/MapPickingZone.action?autocomplete=',
        minChars: 0,
        max: 10,
        width: 150,
        scroll: true,
        cacheLength: 0
    }).focus(function() {
        $(this).autocomplete('search', $(this).val())
    });
});

但是,当我尝试在具有 id 'aisleFrom' 的文本字段中键入任何内容时,我在 jquery-ui.min.js 文件中收到如下错误:

TypeError:this.source 不是函数 ...完成加载"),this.cancelSearch=!1,this.source({term:e},this._response())},...

有人可以建议吗?

【问题讨论】:

  • 您忘记关闭文档准备好的右大括号。
  • 嗨 Ghazali,右大括号存在,但我没有在代码 sn-p 中添加它们
  • 请编辑您的帖子,这会让其他人感到疑惑
  • 看起来你已经包含了两次 jQuery UI,第二次是在自动完成脚本之后
  • 删除第二个 jquery 源会出现如下错误:TypeError: e(...).addClass(...).appendTo(...).menu is not a function

标签: jquery autocomplete


【解决方案1】:

尝试将 url 更改为 source 。见Autocomplete Widget source


  jQuery(document).ready(function() {         
    $("#aisleFrom").autocomplete({
        minLength: 0,
        // substitute `source` for `url`
        source: function(request, response) {
          var term = request.term;
          // get json 
          $.getJSON(/* /path/to/json/ */)
          .then(function success(data) {
            // filter results
            var res = $.grep(data, function(val) {
              return new RegExp($.ui.autocomplete.escapeRegex(term), "gi")
                     .test(val.toLowerCase())
            })
            , key = $.inArray(term.toUpperCase(), res)
            , results = term.length === 1 
                          & key !== -1 
                          ? Array(res[key]) 
                          : res;
            response(results)
          }, function error(jqxhr, textStatus, errorThrown) {
               console.log(textStatus, errorThrown) // log `$.ajax` errors
        })
      }
    }).focus(function() {
          $(this).autocomplete("search", $(this).val())
    });          
  });

jQuery(document).ready(function() {

  $("#tags").autocomplete({
    minLength: 0,
    source: function(request, response) {
      var term = request.term;
      $.getJSON("https://gist.githubusercontent.com/anonymous/86f61fee217838ba6c3c/raw/395a557fa400163f048f30370d782db554913b2b/availableTags.json")
        .then(function success(data) {
          var res = $.grep(data, function(val) {
            return new RegExp($.ui.autocomplete.escapeRegex(term), "gi")
              .test(val.toLowerCase())
          })
          , key = $.inArray(term.toUpperCase(), res)
          , results = term.length === 1 
                        && key !== -1 
                        ? Array(res[key]) 
                        : res;
          response(results)
        }, function error(jqxhr, textStatus, errorThrown) {
             console.log(textStatus, errorThrown) // log `$.ajax` errors
      })
    }
  }).focus(function() {
      $(this).autocomplete("search", $(this).val())
  });
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/jquery-ui-git.js"></script>
<div class="ui-widget">
  <label for="tags">Tags:</label>
  <input id="tags">
</div>

【讨论】:

  • 嗨,我将 url 更改为 source 并且错误消失了。但是,在源参数中指定 URL 字符串时,我无法检索任何结果。
  • 我刚刚在一个项目中使用非常旧的 jQuery UI 版本。正如这个答案中提到的,提供一个函数作为源属性的值,在函数内部执行一个 ajax 请求并将结果作为响应函数中的第一个参数传递。
【解决方案2】:

试试下面的代码。除了 jquery.js 和 jquery-ui.js 之外,您不需要其他 javascript。

$("#aisleFrom").autocomplete({
            source : function(request, response) {
            if ($.trim($(this.element).val()) == "") {
                return;
            }
            $.ui.autocomplete.prototype._renderMenu = function(ul, items) {
                var self = this;
                ul.append("<li><table width='100%' class='table table-condensed' style='margin-bottom:0px;'><tr>"
                                + "<td width='30%'><b>Title</b></td></tr></table></li>");
                $.each(items, function(index, item) {
                    self._renderItem(ul, item);
                });
            };
            $.getJSON("yourURLForDataRetrieving.html", {
                query : $.trim($(this.element).val()),                  
            }, response).error(
                    function(xhr, ajaxOptions, thrownError) {
                        alert(xhr.responseText);
                    });
            },
            open : function() {
             // After menu has been opened, set width to 100px
             $('.ui-menu').width(350);
           },
           minLength : 1,
           select : function(event, ui) {
                //set value in other fields if needed ie. $("#id").val(ui.item.id);
           }
         }).data("autocomplete")._renderItem = function(ul, item) {
                return $("<li></li>")
               .data("item.autocomplete", item)
               .append("<a><table width='100%' class='table table-condensed table-hover' style='margin-bottom:0px;'><tr><td width='30%'>"
                + item.value
                + "</td></tr></table></a>").appendTo(ul);

        };

【讨论】:

    【解决方案3】:

    对我来说很好用。希望对你有帮助:

    <script type="text/javascript">
    $(function() {
        var dados;	
         $.post("sugestao.php",function(data){
    	 dados = data.split(",");
    	 $( "#busca" ).autocomplete({
          source: dados
        });
         });
      
      });
    </script>

    【讨论】:

      猜你喜欢
      • 2020-03-23
      • 2011-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-03
      • 2014-10-10
      相关资源
      最近更新 更多