1

我正在使用 typeahead 进行自动完成。以下代码工作正常。

var employees = new Bloodhound({
    datumTokenizer: function (d) { return Bloodhound.tokenizers.whitespace(d.name); },
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    addOnBlur: true,

    remote: {
        url: $url + '?name=%QUERY',
        global: false,
        wildcard: '%QUERY',
        filter: function (response) {
            return $.map(response.results, function (employee) {

                return {
                    mark: employee.MARK,
                    actitle: employee.ACCOUNTTITLE, 
                    code: employee.CODE
                }
            });
        }
    }

});

employees.initialize();

$($classname).typeahead({ highlight: true, minLength: 1, limit: 5 }, {
    name: 'employees', displayKey: 'mark', source: employees.ttAdapter(), global: false
})
    .on("typeahead:selected", function (obj, company1) {
        //debugger;
    $($retField).val(company1.actitle);
    $($cField).val(company1.code);
})
    .on('focusout', function (obj, company) {
        //debugger;
        $($classname).trigger("typeahead:first-child");
 });

}

我想将 ajax 全局事件设置为 false。请帮我解决问题

4

1 回答 1

0

派对迟到了,但我想通了。使用transport属性:

var employees = new Bloodhound({
    datumTokenizer: function(d) {
        return Bloodhound.tokenizers.whitespace(d.name);
    },
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    addOnBlur: true,
    remote: {
        url: $url + '?name=%QUERY',
        global: false,
        wildcard: '%QUERY',
        transport: function(opts, onSuccess, onError) {
            var url = opts.url.split("#")[0];
            var query = opts.url.split("#")[1];
            $.ajax({
                url: url,
                data: "search=" + query,
                type: "POST",
                success: onSuccess,
                error: onError,
                global: false
            })
        }
        filter: function(response) {
            return $.map(response.results, function(employee) {
                return {
                    mark: employee.MARK,
                    actitle: employee.ACCOUNTTITLE,
                    code: employee.CODE
                }
            });
        }
    }
});
employees.initialize();

基于this other stackoverflow answer

于 2019-06-27T15:19:04.230 回答