0

我想创建一个函数来简化 jQuery UI AutoComplete 的配置。这是我的功能代码:

(function($) {
    $.fn.myAutocomplete = function() {
        var cache = {};
        var dataUrl = args.dataUrl;
        var dataSend = args.dataItem;

        $.autocomplete({
            source: function(request, response) {
                if (cache.term == request.term && cache.content) {
                    response(cache.content);
                }
                if (new RegExp(cache.term).test(request.term) && cache.content && cache.content.length < 13) {
                    var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
                    response($.grep(cache.content, function(value) {
                        return matcher.test(value.value)
                    }));
                }
                $.ajax({
                    url: dataUrl,
                    dataType: "json",
                    type: "POST",
                    data: dataSend,
                    success: function(data) {
                        cache.term = request.term;
                        cache.content = data;
                        response(data);
                    }
                });
            },
            minLength: 2,
        });
    }
}) (jQuery);

但是当我使用这个功能时:

$("input#tag").myAutocomplete({
    dataUrl: "/auto_complete/tag",
    dataSend: { term: request.term, category: $("input#category").val() }
});

它给了我一个错误:

未捕获的 ReferenceError:未定义请求

4

2 回答 2

1

也许错误是指 request.term 在

$("input#tag").myAutocomplete({
    dataUrl: "/auto_complete/tag",
    dataSend: { term: request.term, category: $("input#category").val() }
});
于 2010-04-20T11:06:31.287 回答
0

对不起,我不擅长使用jquery。这是最终的工作代码。

(function($) {
    $.fn.myAutocomplete = function(opt) {
        var cache = {};

        this.autocomplete({
            source: function(request, response) {
                if (cache.term == request.term && cache.content) {
                    response(cache.content);
                }
                if (new RegExp(cache.term).test(request.term) && cache.content && cache.content.length < 13) {
                    var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
                    response($.grep(cache.content, function(value) {
                        return matcher.test(value.value)
                    }));
                }
                opt.dataSend.term = request.term;
                $.ajax({
                    url: opt.dataUrl,
                    dataType: "json",
                    type: "POST",
                    data: opt.dataSend,
                    success: function(data) {
                        cache.term = request.term;
                        cache.content = data;
                        response(data);
                    }
                });
            },
            minLength: 2,
        });
        return this;

    }
}) (jQuery);

要使用此功能,只需编写如下代码:

$("input#tag").myAutocomplete({
    dataUrl: "/auto_complete/tag",
    dataSend: { category: $("input#category").val() }
});

感谢 Jeffery To 与我分享解决这个问题.. ^_^

于 2010-04-20T12:07:09.713 回答