2010-11-08 34 views
1

我有兩個文本框,我試圖用於自動完成。兩者的源數據來自ASP.NET ASMX Web服務,並以JSON格式返回。我又回到這裏NameValue被定義爲一個列表:jQuery自動完成與從ASMX通過JSON的名稱/值對

public struct NameValue { public string Name; public string Value; }

如何可以解析這些數據,使得所選的下拉名稱的值可以存儲起來在一個隱藏字段?

這裏是我的代碼迄今在頁面加載時,和msg.d包含JSON數據(我看到msg.d[0].Namemsg.d[0].Value

$.ajax({ 
     type: "POST", 
     url: '/Services/Team.asmx/GetClubTeams', 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function(msg) { 
      $('#<%= txtFromTeam2.ClientID %>, #<%= txtToTeam2.ClientID %>').autocomplete({ 
      source:msg.d 
      }); 
     }, 
     error: function(xhr, msg) { 
      alert(msg); 
     } 
     }); 

回答

3

回答我自己的問題。關鍵是要知道名稱/值應該是這樣的形式:

public struct TeamData { public string key; public string value; }

看來value(小寫)事項的jQuery。

$.ajax()調用如下...希望這可以幫助別人:

$.ajax({ 
     type: "POST", 
     url: '/Services/Team.asmx/GetClubTeams', 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     data: "{}", 
     success: function(data) { 
     $('#txtFromTeam2').autocomplete({ 
      source: data.d, 
      minLength: 3, 
      focus: function(event, ui) { 
      $('#txtFromTeam2').val(ui.item.value); 
      return false; 
      }, 
      select: function(event, ui) { 
      $('#txtFromTeam2').val(ui.item.value); 
      $('#<%= txtFromTeam2Id.ClientID %>').val(ui.item.key); 
      populatePlayers(ui.item.key); 
      return false; 
      } 
     }); 
     $('#txtToTeam2').autocomplete({ 
      source: data.d, 
      minLength: 3, 
      focus: function(event, ui) { 
      $('#txtToTeam2').val(ui.item.value); 
      return false; 
      }, 
      select: function(event, ui) { 
      $('#txtToTeam2').val(ui.item.value); 
      $('#<%= txtToTeam2Id.ClientID %>').val(ui.item.key); 
      return false; 
      } 
     }); 
     }, 
     error: function(xhr, msg) { 
     alert(msg); 
     } 
    }); 
+1

感謝馬克分享此... – Kwex 2012-02-14 09:09:45