如何使用对象数组作为jQuery UI AutoComplete的源 [英] How can I use a array of objects as source for jQuery UI AutoComplete

查看:82
本文介绍了如何使用对象数组作为jQuery UI AutoComplete的源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有

var arrLinks = [
    { key: 1, url: "http://google.com" },
    { key: 2, url: "http://yahoo.com", title: "Yahoo" },
    { key: 2, url: "http://microsoft.com" }
];

我可以将其用作自动完成的来源吗?我尝试在以下 http://jqueryui.com/demos/autocomplete/#custom-data ,但没有得到它 http://jsfiddle.net/mvNNj/

Can I use it as the source for autocomplete? I tried implementing in following http://jqueryui.com/demos/autocomplete/#custom-data but didn't get it http://jsfiddle.net/mvNNj/

推荐答案

您需要:

1-实际上在您的测试页上包含jQuery + UI.

1 - Actually include jQuery + UI on your test page.

2-结合使用自动完成程序"用于查找匹配项的标签":

2 - Incorporate the use of 'labels' which the Autocompleter uses to find matches:

$(function() {
    var arrLinks = [
        {
        key: 1,
        url: "http://google.com",
        label: 'google'},
    {
        key: 2,
        url: "http://yahoo.com",
        title: "Yahoo",
        label: 'yahoo'},
    {
        key: 2,
        url: "http://microsoft.com",
        label: 'microsoft'}
    ];
    $("input[name=url]").autocomplete({
        source: arrLinks
    }).data("autocomplete")._renderItem = function(ul, item) {
        return $("<li>").data("item.autocomplete", item).append("<a>" + item.url + "</a>").appendTo(ul);
    };
});

您的测试页正在运行.

这篇关于如何使用对象数组作为jQuery UI AutoComplete的源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆