【问题标题】:jQueryUI Autocomplete id valuejQueryUI 自动完成 id 值
【发布时间】:2012-03-29 14:23:41
【问题描述】:

如何在 jQueryUI 自动完成中获取另一个字段(如 Id)而不是给自己贴标签? (在 MVC 剃刀中)

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

【问题讨论】:

  • 你能提供更多细节吗?当用户选择一个值时,你想要input 中的id 吗?
  • 我有一个意见。使用autocomplete,我可以为搜索结果设置标签(如BookTitle),但我可以在结果中获得另一个字段,如BookId

标签: c# asp.net-mvc jquery-ui razor autocomplete


【解决方案1】:

在您的标记中:

<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; 
                    }
                });

【讨论】:

    猜你喜欢
    • 2014-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-28
    • 2013-07-31
    • 1970-01-01
    相关资源
    最近更新 更多