如何使用jQuery自动完成填充额外字段

Dev*_*per 3 javascript asp.net-mvc json autocomplete jquery-autocomplete

我将复杂的JSON数据传递给 jQuery自动完成插件.它工作正常,因此它显示了列表Products.

在此输入图像描述

现在我想得到Price已经包含在JSON数据中的某种方式,当我从autocomlete列表中选择产品时,我想填充input标记Price.

如果有可能,我真的无法得到.我所知道的数据已经是JSON但是如何获得它?

任何线索?

这是JS for jQuery autocomplete插件

 function CreateAutocomplete() {

        var inputsToProcess = $('[data-autocomplete]').each(function (index, element) {
            var requestUrl = $(element).attr('data-action');

            $(element).autocomplete({
                minLength: 1,
                source: function (request, response) {
                    $.ajax({
                        url: requestUrl,
                        dataType: "json",
                        data: { query: request.term },
                        success: function (data) {
                            response($.map(data, function (item) {
                                return {
                                    label: item.Name,
                                    value: item.Name,
                                    realValue: item.UID                                   
                                };
                            }));
                        },
                    });
                },
                select: function (event, ui) {

                    var hiddenFieldName = $(this).attr('data-value-name');
                    $('#' + hiddenFieldName).val(ui.item.UID);
                }
            });
        });
    }
Run Code Online (Sandbox Code Playgroud)

要明确item.LastPricePrice数据.

HTML

   @Html.AutocompleteFor(x => x.ProductUID, Url.Action("AutocompleteProducts", "Requisition"), true, "Start typing Product name...", Model.Product.Name)
Run Code Online (Sandbox Code Playgroud)

dco*_*ith 5

在您的ui.item对象中,您应该能够Price在那里找到该属性,然后在select函数中设置该值.

success: function (data) {
    response($.map(data, function (item) {
        return {
            label: item.Name,
            value: item.Name,
            realValue: item.UID,
            price: item.LastPrice // you might need to return the LastPrice here if it's not available in the ui object in the select function
        };
    }));
},
..

select: function (event, ui) {
   var hiddenFieldName = $(this).attr('data-value-name'),
       unitPriceEl = $('#price');
   $('#' + hiddenFieldName).val(ui.item.UID);
   unitPriceEl.val(ui.item.LastPrice); //set the price here
}
Run Code Online (Sandbox Code Playgroud)