首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jQuery UI -> autocomplete -> ON select -> ui item is undefined

jQuery UI -> autocomplete -> ON select -> ui item is undefined
EN

Stack Overflow用户
提问于 2012-11-30 00:47:58
回答 1查看 12.4K关注 0票数 5

我在我的页面上有一个自动完成程序,它可以正确地获取和显示数据...当它停止正常工作时处于select事件....

代码语言:javascript
复制
$("#fld_search1").catcomplete({
        delay: 50,
        minLength: 2,
        open: function(e, ui){
            if(searching) return;
            //console.log($(this).data('catcomplete'));
            var acData = $(this).data('catcomplete');
           var styledTerm = '<strong>%s</strong>'.replace('%s', acData.term);


            acData.menu
                .element
                .find('li a')
                .each(function() {
                    var me = $(this);
                    me.html( me.text().replace(acData.term, styledTerm) );
                });
            //return match.replace(new RegExp("("+keywords+")", "gi"),'<i>$1</i>');
        },
        select: function(event, ui) {
            var I = ui.item;
            top.console.log(ui);
            $("#fld_search1" ).catcomplete("close");

            $('#fld_search1').val(I.name);
            window.location = '/podjetje/'+I.value+'.html';
            //$('#frm_company_id').val(I.value);
            return false;
        },
        source: function( request, response ) {


            search_term = request.term;

            if ( search_term in cache ) {
                response( cache[ tesearch_termrm ] );
                return;
            }


            var suggest_url = "/companies/find_company.json";


            $.ajax({
                url: suggest_url,
                dataType: "json",
                type : "POST",
                data: {
                    owner: request.term
                },
                success: function( data ) {
                    response( $.map( data, function( item ) {
                        var alabel = item.label.replace(
                                new RegExp('(' +
                                    $.ui.autocomplete.escapeRegex(request.term) +
                                    ')'), 
                            "<b>$1</b>" );
                        return {
                            value: item.value,
                            label: item.label,
                            name: item.name,
                            category: item.category
                        }
                    }));
                }
            });


        }
    });

所以它不会获取ui对象...

如果我执行top.console.log(ui),我会得到一个只有一个属性的对象->item...这是不确定的..。所以如果我记录下我得到的未定义的I值...这怎麽可能?

这是在1.9.1中

如果我更改它并使用1.9.2,菜单总是在鼠标悬停时关闭...如果我使用autoFocus,它甚至打不开!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-04 23:08:56

今天,我遇到了与未定义的ui.item属性相同的问题。经过一些调试,我找到了解决方案。jQuery UI团队对Autocomplete with categories示例代码做了一些修改。当您查看示例源代码时,您将看到:

代码语言:javascript
复制
<script>
    $.widget( "custom.catcomplete", $.ui.autocomplete, {
        _renderMenu: function( ul, items ) {
            var that = this,
                currentCategory = "";
            $.each( items, function( index, item ) {
                if ( item.category != currentCategory ) {
                    ul.append( "<li class='ui-autocomplete-category'>" + item.category + "</li>" );
                    currentCategory = item.category;
                }
                that._renderItemData( ul, item );
            });
        }
    });
    </script>

在1.9之前的版本中,带有that._renderItemData( ul, item );的代码行曾经是that._renderItem( ul, item );。另请参阅:bug #8560。但在1.9 Upgrade Guide中没有提到这一点。

所以我把我的插件改成使用_renderItemData(...,...)函数,这样就解决了问题。

希望这能有所帮助!

票数 28
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13630617

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档