我有两个文本框,我尝试用于自动完成。两者的源数据来自ASP.NET ASMX Web服务,以JSON格式返回。我正在返回一个List,其中NameValue定义为:
public struct NameValue { public string Name; public string Value; }
如何解析这些数据,以便将所选下拉名称的值存储在隐藏字段中?
这是我的代码到目前为止加载页面时,msg.d
包含JSON数据(我看到msg.d[0].Name
和msg.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);
}
});
答案 0 :(得分: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);
}
});