jQuery UI自动完成 - 修改问题

时间:2012-09-12 09:43:07

标签: javascript jquery jquery-ui jquery-ui-autocomplete

查看jQuery UI自动完成:http://jqueryui.com/demos/autocomplete/并更改它以满足我的要求,我遇到了一些问题。

如果您查看链接的默认列表,其中一个选项是“ActionScript”,如果您键入“Sc”,则表示“ActionScript”,在我的情况下不适合。

我只希望它建议“ActionScript”,例如,如果用户键入:

  • A
  • AC
  • ACTI
  • 的Actio
  • 动作
  • 操作
  • ActionSc
  • ActionScr
  • ActionScri
  • ActionScrip
  • 的ActionScript

“ActionScript”只是一个例子,但它得到了重点。

查看jQuery UI自动完成代码中的suggest函数:

_suggest: function( items ) {
        var ul = this.menu.element
            .empty()
            .zIndex( this.element.zIndex() + 1 );
        this._renderMenu( ul, items );
        // TODO refresh should check if the active item is still in the dom, removing the need for a manual deactivate
        this.menu.deactivate();
        this.menu.refresh();

        // size and position menu
        ul.show();
        this._resizeMenu();
        ul.position( $.extend({
            of: this.element
        }, this.options.position ));

        if ( this.options.autoFocus ) {
            this.menu.next( new $.Event("mouseover") );
        }
    },

我似乎无法找到缩小选择范围的部分。有人能指出我正确的方向吗?我正在使用最新的稳定版本。

1 个答案:

答案 0 :(得分:1)

function hackAutocomplete(){

    $.extend($.ui.autocomplete, {
        filter: function(array, term){
            var matcher = new RegExp("^" + term, "i");

            return $.grep(array, function(value){
                return matcher.test(value.label || value.value || value);
            });
        }
    });
}

hackAutocomplete();

找到解决此问题的代码。