数据绑定到选择

时间:2012-10-19 19:00:21

标签: jquery select jquery-mobile knockout.js

我有一个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 () { });
    }

1 个答案:

答案 0 :(得分:1)

您只需设置select元素的正确属性即可 api会为你做的。

<select data-bind="options: ReferenceTypes, optionsText: 'TeamName', 
optionsCaption: 'Select...', optionsValue: 'Id', **value:TeamModel**">
</select>

例: knockoutjs mapping select data-bind options

http://jsfiddle.net/madcapnmckay/Ba5gx/