jQuery的自动完成功能。数据(QUOT;自动完成")是未定义 [英] jQuery Autocomplete .data("autocomplete") is undefined

查看:103
本文介绍了jQuery的自动完成功能。数据(QUOT;自动完成")是未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我试图实现自动完成使用code以下,我得到一个错误,说明:

 。数据(自动完成)是未定义

如何过,如果我从末端取下的.data()方法,它工作正常(只是出的.data()提供了定制的图形)。谁能告诉我这是怎么回事了?

  $(#输入testInput)。绑定(autocompleteselect功能(事件,UI){  })。自动完成({
      appendTo:#autoCompList
      来源:函数(请求,响应){
          $阿贾克斯({              网址:JSONP CALL网址
              数据类型:JSONP
              数据:{
                  要素类:P
                  风格:全,
                  maxRows进行:12,
                  name_startsWith:request.term
              },
              成功:功能(数据){
                  响应($。图(数据。数据,功能(项目){
                      fbPageJson =项目;
                          返回{
                              标签:item.name,
                              图片:item.picture,
                              JSON:项目,
                          }
                  }));
              },
          });
      }  })。数据(自动完成)._ renderItem =功能(UL,项目){
      返回$(<李>< /李>)的数据(item.autocomplete项目).append(< A>< IMG SRC ='+ item.image +'ALT = 无照片/>&下; / A>中+ item.label).appendTo(微升);
  };


解决方案

我曾基础上,1.10.0版本的jQuery用户界面的同样的问题,我想你应该尝试

 数据('uiAutocomplete')

而不是

 数据(自动完成)

根据约翰尼的评论,我查了。数据()函数是如何工作的。是的,jQuery的从返回空值。数据()调用时选择没有找到任何物品。

因此​​,如果存在用于您的选择没有匹配元件,则没有自动填充对象被创建并添加到自定义数据对象。

所以,现在看来,这是更好地做到这一点:

  $(选择).autocomplete({您自动完成配置的道具在这里});
    如果($(选择器)。数据()){    //一些jQueryUI的版本可能使用不同的密钥对的对象。所以要确保,
    //把一个断点以下行并添加手表$(选择器)。数据()。
    //那么你可以找出密钥由您jQueryUI的脚本。        VAR AC = $(选择器)。数据(uiAutocomplete');
        如果(交流){
           //你想自动完成的对象是什么。下面是更改版本为例,从jQueryUI的自动完成教程           ac._renderItem =功能(UL,项目){
                返回$(<立GT;)
                    .append(&所述a取代;+ item.label +&下; / A>中)
                    .appendTo(微升);
            };
        }
    }

When I try to implement auto-complete using the code below I get an error stating:

.data("autocomplete") is undefined

How ever if I remove the .data() method from the end it works fine (just with out the customizable graphics that .data() provides). Can anyone tell me what's going wrong?

$("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);
  };

解决方案

I had the same problem and based on the 1.10.0 version of jquery ui, I think you should try

data('uiAutocomplete')

instead of

data('autocomplete')

Based on Johnny's comment, I checked how the .data() function works. Yes, jQuery returns null from .data() call when selector does not find any items.

So if there is no matching element for your selector, then no autocomplete object is created and added to the custom data object.

So it seems it is better to do this:

    $(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的自动完成功能。数据(QUOT;自动完成&QUOT;)是未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆