jQueryUI自动完成id值

本文关键字:id jQueryUI | 更新日期: 2023-09-27 18:27:30

如何在jQueryUI自动完成中获得另一个字段(如Id),而不是标记自己?(在MVC Razor中)

我使用autocomplete,它很有效,但除了标签之外,我还想要一个字段(或者可能不止一个字段)。

jQueryUI自动完成id值

在标记中:

<input type="text" class="item" id="input_autocomplete_1" custom_attr="XXX" />

在你的js:

     $('.item').autocomplete({
                    source: function( request, response ) {
//to send the custom attribute to the populate function
                        customAttr = this.element.attr('custom_attr');
                        $.ajax({
                            url: "",
                            dataType: "json",
                            data: {
                                term : request.term,
                                custom_attr: customAttr 
                            },
                            success: function( data ) {
//return a json with label, id and custom
                                response( $.map( data, function( item ) {
                                    return {
                                        label: item.label,
                                        id: item.id,
                                        custom: item.custom
                                    }
                                }));
                            }
                        });
                    },
                    select: function( event, ui ) {
//if you want to do something when the item is selected
                       //you can access:
                       //ui.item.id;
                       //ui.item.custom; 
                    }
                });