0

我在视图上使用了几个 jQuery 自动完成功能来检索 CustomerName 和 CustomerID。

$(document).ready(function () {
    $('#CustomerByName').autocomplete({
        source: function (request, response) {
            $.ajax({
                url: "/Cases/FindByName",
                type: "GET",
                dataType: "json",
                data: {
                    searchText: request.term,
                    maxResults: 10
                },
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    response($.map(data, function (item) {
                        return {
                            label: item.CustomerName,
                            value: item.CustomerName,
                            id: item.CustomerID
                        }
                    }));
                }
            });
        },
        minLength: 1,
        select: function (event, ui) {
            $('#CustomerID').val(ui.item.id);
        },
    });
});

成功响应给了我标签、值、id。有没有办法访问 json 数据中返回的其他字段?在这种情况下,整个表将作为 json 返回。

谢谢

4

1 回答 1

2

修改成功函数以包含您要在选择中使用的任何额外字段,例如

success: function (data) {
              response($.map(data, function (item) {
                  return {
                      label: item.CustomerName,
                      value: item.CustomerName,
                      id: item.CustomerID,
                      // extra fields go here
                      address: item.CustomerAddress
                  }
              }));
          }

然后你可以像这样在 select 函数中访问它们:

select: function (event, ui) {
    $('#CustomerID').val(ui.item.id);
    alert('CustomerAddress is ' + ui.item.address);
},
于 2012-06-24T21:48:20.710 回答