jQueryUI Значение идентификатора автозаполнения - PullRequest
0 голосов
/ 13 марта 2012

Как получить другое поле (например, Id) в jQueryUI Autocomplete вместо самой метки? (В MVC Razor)

Я использую autocomplete, и это работает, но мне нужно поле (или, может быть, более одного поля) в дополнение к метке.

1 Ответ

0 голосов
/ 13 марта 2012

В вашей разметке:

<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; 
                    }
                });
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...