2014-10-28 40 views
0

我需要一個組合框(ID /名稱)對,用戶選擇一個項目,並將ID發送到服務器。 同時組合框調用AJAX web服務與自動完成選擇JQuery UI自動完成組合框選擇值而不是名稱

我有一個差不多的工作例如,在此JSFiddle

這裏是AJAX部分:

source: function(request, response) { 
       $.ajax({ 
        url: "http://ws.geonames.org/searchJSON", 
        type: "POST", 
        dataType: "jsonp", 
        data: { 
         featureClass: "P", 
         style: "full", 
         username: "andrebarata", 
         maxRows: 12, 
         name_startsWith: request.term 
        }, 
        success: function(data) { 
         response($.map(data.geonames, function(item) { 
          return { 
           label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName, 
           value: item.geonameId 
          } 
         })); 
        } 
       }) 
      }, 

唯一的問題是當我選擇一個值時,它會將值更改爲ID而不是名稱。

我該怎麼做才能改變這種情況?

+0

我想知道解決方案是否可以在每個自動完成項目中存儲一個(名稱/值)對,然後再取回我需要的一個 – 2014-10-28 13:28:37

回答

1

如果我改變:由「價值item.geonameId」「值:item.name」它的工作...

,並獲得ID添加一個新屬性「大地水準面」是這樣的:

success: function(data) { 
         response($.map(data.geonames, function(item) { 
          return { 
           label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName, 
           value: item.name, 
           geoid: item.geonameId 
          } 
         })); 
        } 
       }) 
      }, 
      select: function(event, ui) { 
       alert(ui.item.value+" , "+ ui.item.geoid); 
+0

是的,但ID丟失了,如您在彈出消息中看到的。 – 2014-10-28 13:26:02

+0

我認爲自動填充小部件只存儲一個簡單的列表,而不是一個(名稱/值)對列表 – 2014-10-28 13:26:59

+0

好的,但你可以通過添加一個像geoid這樣的新屬性,並把你的id數據放在裏面。所以你有這個:「..value:item.name,geoid:item.geonameId ...」,並在警告消息:「ui.item.value +」,「+ ui.item.geoid」 – tordo256 2014-10-31 14:21:34