jQuery自动完成而不使用标签,值,ID [英] JQuery Autocomplete without using label, value, id

查看:89
本文介绍了jQuery自动完成而不使用标签,值,ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在不使用常规id,label,value的情况下实现JQuery自动完成功能?

Is it possible to have JQuery Autocomplete work without using conventional id,label,value?

例如,我不想使用:

[ { "id": "Botaurus stellaris", "label": "Great Bittern", "value": "Great Bittern" }, 
{ "id": "Asio flammeus", "label": "Short-eared Owl", "value": "Short-eared Owl" }]

但是:

[ { "my_id": "Botaurus stellaris", "first_name": "Great Bittern", "last_name": "Great Bittern" }, 
{ "my_id": "Asio flammeus", "first_name": "Short-eared Owl", "last_name": "Short-eared Owl" }]

例如,JQuery autocomplete通常需要使用什么id,value,label,我可以使其接受my_id, first_name, last_name并使Jquery自动完成功能像id,label,value一样对待吗?

So like, what JQuery autocomplete normally takes id,value,label, can I make it to take my_id, first_name, last_name and make Jquery Autocomplete treat like same id,label,value?

当我不想修改来自数据源的数据的键以将其显示在JQuery Autocomplete上时,这可能很有用.

This might be useful, when I don't wish to modify data's keys coming from a datasource, to display it on JQuery Autocomplete.

推荐答案

没关系,

从JQueryUI的自动完成"示例源中找到了答案:

Found the answer from JQueryUI's Autocomplete example source:

    $( "#city" ).autocomplete({
        source: function( request, response ) {
            $.ajax({
                url: "http://ws.geonames.org/searchJSON",
                dataType: "jsonp",
                data: {
                    featureClass: "P",
                    style: "full",
                    maxRows: 12,
                    name_startsWith: request.term
                },
                success: function( data ) {
                    response( $.map( data.geonames, function( item ) {
                        return {
                            label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
                            value: item.name
                        }
                    }));
                }
            });
        },
        minLength: 2
    });

在这里,source被分配了一个函数,该函数可以转换并返回需要转换为JQuery的自动完成的id,value,label的任何类型的键.

Here, source is been assigned a function, that can convert and return any type of keys to required be converted to JQuery's Autocomplete's id,value,label.

也许对某些人有帮助.

这篇关于jQuery自动完成而不使用标签,值,ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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