如何在我的自定义函数中使用Jquery UI? (自动填充)

时间:2010-04-20 10:50:37

标签: javascript jquery jquery-ui-plugins

我想创建一个简化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:未定义请求

2 个答案:

答案 0 :(得分:1)

错误可能是指

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

答案 1 :(得分: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与我分享以解决此问题.. ^ _ ^