0

我有一个看起来像这样的模型视图:

var teamViewModel = {
        teams: ko.observableArray([]),
        selectedTeam: ko.observable({id: 1}),
        clearTeams: function(){
            this.teams.removeAll();
        },
        addTeam: function (id, name, isChecked) {
            t = new team(id, name, isChecked);
            this.teams.push(t);
        }
    };

我在这里引用它:

<select id="teamNameLabel" date-theme="f" data-bind="options: teams, optionsText: 'id', value: selectedTeam"></select>

当我选择其中一个团队时,我可以点击一个按钮:

<input id="TestButton" type="button" onclick="GetAllUsersByTeam()" value="Show Selected Team" />

它会这样称呼:

function GetAllUsersByTeam(){

        var url = 'http://localhost/Service.svc/GetUsersByTeam/'+teamViewModel.selectedTeam().id;

        var jqxhr =
        $.getJSON(url,
        function (data) 
        { 
            colleagueViewModel.clearColleagues();
            $.each(data.GetUsersByTeamResult, function (key, val) {
                colleagueViewModel.addColleague(val.FirstName, val.LastName, val.EmailAddress, val.PhoneNumber, val.LocationName, val.CapabilityId, val.CoeId);
            });
            ko.applyBindings(colleagueViewModel, document.getElementById("colleaguesListView"));
        })
    }

这就像我想要的一样。但我想更改,以便将 GetAllUsersByTeam() 称为 onchange。然而:

<select id="teamNameLabel" date-theme="f" onchange="GetAllUsersByTeam()" data-bind="options: teams, optionsText: 'id', value: selectedTeam"></select>

总是迟到 1 个 ID。例如,当我选择 1 时,它的 id 是 1,2 id 是 1,3 id 是 2 ......我该如何解决这个问题?

4

1 回答 1

2

selectedTeam为什么不在你的模型中订阅呢?

this.selectedTeam.subscribe(function(newValue) {
    GetAllUsersByTeam();
});
于 2012-10-22T20:56:56.437 回答