2011-09-09 50 views
0

我打電話給遠程數據源並取回json。在我選擇一個項目後,select:回調選項只允許我處理項目的標籤和值,但我還想使用我的json對象的其他屬性來自動填充其他字段。選擇jQuery自動完成填寫其他字段

有沒有一個方便的方法來做到這一點,我失蹤了?我看到到目前爲止是選項...

  1. 緩存全局Ajax響應JSON對象和參考選擇
  2. 重新查詢後這個全局對象使用該項目的價值或標籤
在選擇數據庫

對這些都不是特別滿意。思考?

編輯 我忘了我是用$ .MAP

$('#accountName').autocomplete({ 
    source: function (request, response) { 

     $.getAccountsByNameLike(request.term, function (data) { 
      response($.map(data, function (item) { 
       return { 
        label: item.Name + ' (' + item.Address.City + ', ' + item.Address.StateOrProvince + ')', 
        value: item.AccountId, 
        // Added to fix issue 
        raw: item 
       } 
      })); 
     }, function (error) { 
      // async kickoff a log to logging server service... 
      alert("There was a problem while trying to retrieve account names. Please contact support"); 
     }); 

回答

4

是什麼讓你覺得你可以只用標籤和值工作?

select: function (event, ui) { 
     foo = ui.item.label; 
     $("#bar").val(ui.item.id); 
     baz = (ui.item.JsonField); 
    } 
相關問題