我有一个jquery函数从服务中获取所有团队。我如何将其数据绑定到选择框?
function GetAvailableTeams() {
$.support.cors = true;
var jqxhr =
$.getJSON('http://localhost/Service.svc/GetTeamsAll',
function (data) {
teamViewModel.clearTeams();
$.each(data.GetTeamsAllResult,
function (key, val) {
teamViewModel.addTeam(val.TeamId, val.TeamName, true);
}
ko.applyBindings(teamViewModel, document.getElementById("teamNameLabel"));
})
.success(function () { })
.error(function () { alert("Encountered error with the service: GetTeams. "); })
.complete(function () { });
}
答案 0 :(得分:1)
您只需设置select元素的正确属性即可 api会为你做的。
<select data-bind="options: ReferenceTypes, optionsText: 'TeamName',
optionsCaption: 'Select...', optionsValue: 'Id', **value:TeamModel**">
</select>