未捕获的TypeError:undefined不是函数jquery自动完成

时间:2014-04-21 08:08:27

标签: javascript jquery jquery-ui autocomplete

我正在尝试使用jquery自动完成组件。我想在autocomplete.so中显示图像我必须写_renderMenu和_renderIItem function.i写了一个代码:

 var _filterUser = $('#filterUser').autocomplete({
                    source: onEditLoadUsers,
                    select: onEditSelectUser,
                    focus: onEditFocusUser
                }); 
 $('#filterUser').data("autocomplete")._renderItem= function( ul, item ){
    console.log('coming here');
    return $( "<li>" )
           .append( $( '<img src="'+item.photo+'" />' ) )
           .appendTo( ul );
 };
 $('#filterUser').data("autocomplete")._renderMenu= function(ul,items){
     var that = this;
     console.log('ul is')
     console.log(ul);
     console.log(items);
     $.each( items, function( index, item ) {
         that._renderItemData( ul, item );
     });                       
 };

如果我删除_renderItem函数和_renderMenu函数然后简单的自动完成工作正在工作但是当我添加这两个函数时它给了我错误:

Uncaught TypeError: undefined is not a function FullCalendar?sfdc.tabName=01ri0000000sqRT:359
(anonymous function) FullCalendar?sfdc.tabName=01ri0000000sqRT:359
v.extend.each jquery.min.js:2
$.data._renderMenu FullCalendar?sfdc.tabName=01ri0000000sqRT:358
a.widget._suggest jquery-ui.min.js:5
a.widget.__response jquery-ui.min.js:5
(anonymous function) jquery-ui.min.js:5
onEditLoadUsers FullCalendar?sfdc.tabName=01ri0000000sqRT:779
a.widget._search jquery-ui.min.js:5
a.widget.search jquery-ui.min.js:5
(anonymous function)

我正在使用jquery 1.8.3和jquery UI 1.8。请指导如何解决此错误?

1 个答案:

答案 0 :(得分:1)

您不能以这种方式扩展对象。

尝试这种方式:

(function ($, undefined) {
    var ac = $.ui.autocomplete.prototype;
    if (typeof $.uix !== "object") { $.uix = {}; }

    ac = $.extend({}, ac, {
        _renderItem: function (ul, item) {
            console.log('coming here');
            return $("<li>")
                .append($('<img src="' + item.photo + '" />'))
                .appendTo(ul);
        },
        _renderMenu: function (ul, items) {
            var that = this;
            console.log('ul is')
            console.log(ul);
            console.log(items);
            $.each(items, function (index, item) {
                that._renderItemData(ul, item);
            });
        }
    });

    $.uix.autocomplete = ac;
    $.widget("uix.autocomplete", $.uix.autocomplete);
})(jQuery);