jquery 自动完成源代码说明

jquery autocomplete source code explanation

本文关键字:源代码 说明 jquery      更新时间:2023-09-26

我想让jquery的自动完成工作到自定义事件。可能我可能需要调整源代码。我试图理解它,但不确定'function( request, response )'是如何工作的。它是如何被调用的,参数来自哪里?

_initSource: function() {
        var array, url,
            that = this;
        if ( $.isArray(this.options.source) ) {
            array = this.options.source;
            this.source = function( request, response ) {
                response( $.ui.autocomplete.filter( array, request.term ) );
            };
        } else if ( typeof this.options.source === "string" ) {
            url = this.options.source;
            this.source = function( request, response ) {
                if ( that.xhr ) {
                    that.xhr.abort();
                }
                that.xhr = $.ajax({
                    url: url,
                    data: request,
                    dataType: "json",
                    success: function( data ) {
                        response( data );
                    },
                    error: function() {
                        response( [] );
                    }
                });
            };
        } else {
            this.source = this.options.source;
        }
    }

它是从自动完成中的不同方法调用的。

像这里 :

this.source( { term: value }, this._response() );

这是_search方法。