jQuery   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了jQuery Autocomplete .data(“autocomplete”)未定义大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
当我尝试实现自动完成使用下面的代码,我得到一个错误说:
.data("autocomplete") is undefined

如果我从最后删除.data()方法,它工作正常(只是与出的可定制的图形.data()提供)。任何人都可以告诉我发生了什么问题?

$("input#tesTinput").bind("autocompleteSELEct",function (event,ui) {

  }).autocomplete({
      appendTo: "#autoCompList",source: function (request,responsE) {
          $.ajax({

              url: JSONP CALL URL
              dataType: "jsonp",data: {
                  featureClass: "P",style: "full",maxRows: 12,name_startsWith: request.term
              },success: function (data) {
                  response($.map(data.data,function (item) {
                      fbPageJson = item;
                          return {
                              label: item.name,image: item.picture,json: item,}
                  }));
              },});
      }

  }).data("autocomplete")._renderItem = function (ul,item) {
      return $("<li></li>").data("item.autocomplete",item).append("<a><img src='" + item.image + "' alt='no photo'/></a>" + item.label).appendTo(ul);
  };

解决方法

我有同样的问题,并基于1.10.0版本的jquery ui,我想你应该试试
data('uiAutocomplete')

代替

data('autocomplete')

基于Johnny的评论,我检查了.data()函数的工作原理。是的,当选择器没有找到任何项目时,jQuery从.data()调用返回null。

因此,如果您的选择器没有匹配元素,则不会创建自动完成对象,并将其添加自定义数据对象。

所以它似乎是更好地这样做:

$(SELEctor).autocomplete({ your autocomplete config props here });
    if ( $(SELEctor).data() ) {

    // some jQueryUI versions may use different keys for the object. so to make sure,// put a breakpoint on the following line and add a watch for $(SELEctor).data(). 
    // then you can find out what key is used by your jQueryUI script.

        var ac = $(SELEctor).data('uiAutocomplete');
        if ( ac ) {
           // do what you want with the autoComplete object. below is the changed version of an example from jqueryUI autocomplete tutorial

           ac._renderItem = function(ul,item) {
                return $("<li>")
                    .append("<a>" + item.label + "</a>")
                    .appendTo(ul);
            };
        }
    }

大佬总结

以上是大佬教程为你收集整理的jQuery Autocomplete .data(“autocomplete”)未定义全部内容,希望文章能够帮你解决jQuery Autocomplete .data(“autocomplete”)未定义所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。