2012-06-24 136 views
0

我在視圖上使用幾個jQuery自動完成功能來檢索CustomerName和CustomerID。jquery自動完成返回值

$(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返回。

感謝

+0

你有標籤,沒有必要將它們添加到標題。 – gdoron

+0

感謝您的回答。我不明白你的意思。我是jquery的新手。 – Ryan

回答

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: function (event, ui) { 
    $('#CustomerID').val(ui.item.id); 
    alert('CustomerAddress is ' + ui.item.address); 
},