【问题标题】:jQuery UI autocomplete (combobox): how to fill it with the result of an AJAX request?jQuery UI 自动完成(组合框):如何用 AJAX 请求的结果填充它?
【发布时间】:2011-11-29 10:40:22
【问题描述】:

是否可以像往常一样使用combobox jquery-ui ajax autocomplete field

我需要什么?

我希望有一些默认选项,当用户尝试输入任何字母时,它必须连接到服务器以查找请求的信息(像往常一样远程 json 自动完成)。

有可能吗?

【问题讨论】:

  • 底层元素是否为select(就像在组合框示例中一样)是否重要?
  • 不,它不必在那里。

标签: jquery-ui jquery-ui-autocomplete


【解决方案1】:

这是 jQueryUI 示例的一个经过大量修改的版本 (gist):

$.widget("ui.combobox", {
    _create: function() {
        var _self = this
            , options = $.extend({}, this.options, {
            minLength: 0,
            source: function(request, response) {
                if (!request.term.length) {
                    response(_self.options.initialValues);
                } else {
                    if (typeof _self.options.source === "function") {
                        _self.options.source(request, response);
                    } else if (typeof _self.options.source === "string") {
                        $.ajax({
                            url: _self.options.source,
                            data: request,
                            dataType: "json",
                            success: function(data, status) {
                                response(data);
                            },
                            error: function() {
                                response([]);
                            }
                        });
                    }
                }
            }
        });

        this.element.autocomplete(options);

        this.button = $("<button type='button'>&nbsp;</button>")
            .attr("tabIndex", -1)
            .attr("title", "Show All Items")
            .insertAfter(this.element)
            .button({
                icons: {
                    primary: "ui-icon-triangle-1-s"
                },
            text: false
            })
            .removeClass("ui-corner-all")
            .addClass("ui-corner-right ui-button-icon")
            .click(function() {
                if (_self.element.autocomplete("widget").is(":visible")) {
                    _self.element.autocomplete("close");
                    return;
                }
                _self.element.autocomplete("search", "");
                _self.element.focus();
        });
    }
});

用法:

$("input_element_selector").combobox({
    initialValues: ['array', 'of', 'values'],
    source: /* <-- function or string performing remote search */,
    /* any other valid autocomplete options */
});

示例: http://jsfiddle.net/Jpqa8/

当搜索长度为“0”时,小部件使用提供的initialValues 数组作为源(当用户单击下拉按钮时会发生这种情况)。

提供执行远程搜索的source 参数(函数或字符串)。您还可以使用通常与自动完成小部件一起使用的任何其他选项。

【讨论】:

  • 唯一的问题是如何将初始值作为对象而不是数组?例如:{'1':'array', '6':'of', '9':'values'}。因为当用户选择任何初始值时,它必须返回它的 id 值(就像通常的选择一样)。
  • 看起来我没听懂你的澄清问题(我的英语很差)。对不起...
  • @VitaliPonomar:您可以提供这样的数组:[{ 'label': 'array', 'value': '1'}, {'label': 'of', 'value': '6'}, {'label': 'values', 'value': '9'}]
  • 唯一的事情是当我选择一些初始值时,它会自动将其替换为它的数字...jsfiddle.net/eeMfN(我想要的是:初始值是某些国家/地区最受欢迎的城市)跨度>
  • 啊,我明白了。好吧,您可以这样做,这样当您选择一个项目时,它会显示label 而不是value,但随后value 将无法正确发布...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-29
  • 2011-04-07
  • 2011-11-16
  • 1970-01-01
  • 1970-01-01
  • 2011-09-21
相关资源
最近更新 更多