如何在下拉淘汰赛js中预先选择一个选项

发布于 2024-12-20 08:27:44 字数 1904 浏览 0 评论 0原文

我已经查看了其他问题,但无法让我的选择框正常工作: 绑定下拉(选择)列表的初始/默认值< /a>

我有以下 Game 对象:

function Game(visitingTeamDetails, homeTeamDetails, game) {
if (arguments.length > 0) {
    this.VisitingTeamDetails = visitingTeamDetails;

    this.HomeTeamDetails = homeTeamDetails;

    this.GameId = ko.observable(game.GameId);
    this.HomeTeamName = ko.observable(game.HomeTeamName);
    this.VisitingTeamName = ko.observable(game.VisitingTeamName);
    this.SportTypeName = ko.observable(game.SportTypeName);
    this.HomeAccountName = ko.observable(game.HomeAccountName);
    this.VisitingAccountName = ko.observable(game.VisitingAccountName);
    this.GameDateString = ko.observable(game.GameDateString);
    this.GameTimeString = ko.observable(game.GameTimeString);
    this.AvailableSportTypes = ko.observableArray(game.Sports);

    this.sportTypeFunction = function () {
        for (sportType in this.AvailableSportTypes()) {
            if (this.AvailableSportTypes()[sportType].Name == this.SportTypeName()) {
                return this.AvailableSportTypes()[sportType];
            }
        }
        return null;
    };

    this.SportType = ko.observable(game.SportType);
}
}

SportType 是一个具有 NameSportTypeId 的对象。

我有以下模板:

 <td rowspan="3"><select data-bind="options: AvailableSportTypes, value: SportType, optionsText:'Name', optionsCaption: 'Choose...'" class="sportType"></select></td>

AvailableSportTypesSportType 的列表。

该列表在下拉列表中包含 SportType 的名称,但我无法将初始选择设置为 SportType。我编写了 sportTypeFunction 来向自己表明数据输入正确,并且它会选择正确的值,但更改下拉列表中的选择不会更新 SportType。

我确信我做错了什么。有人看到吗?

谢谢

I've looked at this other question, but can't get my select box to work correctly:
Binding initial/default value of dropdown (select) list

I've got the following Game object:

function Game(visitingTeamDetails, homeTeamDetails, game) {
if (arguments.length > 0) {
    this.VisitingTeamDetails = visitingTeamDetails;

    this.HomeTeamDetails = homeTeamDetails;

    this.GameId = ko.observable(game.GameId);
    this.HomeTeamName = ko.observable(game.HomeTeamName);
    this.VisitingTeamName = ko.observable(game.VisitingTeamName);
    this.SportTypeName = ko.observable(game.SportTypeName);
    this.HomeAccountName = ko.observable(game.HomeAccountName);
    this.VisitingAccountName = ko.observable(game.VisitingAccountName);
    this.GameDateString = ko.observable(game.GameDateString);
    this.GameTimeString = ko.observable(game.GameTimeString);
    this.AvailableSportTypes = ko.observableArray(game.Sports);

    this.sportTypeFunction = function () {
        for (sportType in this.AvailableSportTypes()) {
            if (this.AvailableSportTypes()[sportType].Name == this.SportTypeName()) {
                return this.AvailableSportTypes()[sportType];
            }
        }
        return null;
    };

    this.SportType = ko.observable(game.SportType);
}
}

SportType is an object with Name and SportTypeId.

I have the following template:

 <td rowspan="3"><select data-bind="options: AvailableSportTypes, value: SportType, optionsText:'Name', optionsCaption: 'Choose...'" class="sportType"></select></td>

AvailableSportTypes is a list of SportType.

The list is coming in with the names of the SportTypes in the drop down list, but I can't make the initial selection be SportType. I wrote sportTypeFunction to show myself that the data was coming in correctly, and it would select the correct value, but changing my selection in the drop down would not update SportType.

I'm sure I'm doing something wrong. Anyone see it?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷弦 2024-12-27 08:27:44

当传入 game.SportType 时,它需要是对 game.AvailableSportTypes 中项目的引用,而不仅仅是看起来相同的对象。

基本上两个对象不相等,除非它们实际上是对同一对象的引用。

var a = { name: "test" },
    b = { name: "test" }; 

    alert(a === b); //false

因此,您需要调用函数来在数组中找到正确的对象并将其设置为可观察值的值。

并不是说它更好,而是在 KO 1.3 中,您可以扩展 observables、observableArrays 和 dependentObservables 的 .fn 来添加额外的功能。

这是一个示例: http://jsfiddle.net/rniemeyer/ZP79w

When game.SportType gets passed in, it needs to be a reference to the an item in the game.AvailableSportTypes and not just an object that looks the same.

Basically two objects are not equal unless they are actually a reference to the same object.

var a = { name: "test" },
    b = { name: "test" }; 

    alert(a === b); //false

So, you would need to call your function to locate the correct object in the array and set it as the value of your observable.

Not that it is way better, but in KO 1.3 you can extend .fn of observables, observableArrays, and dependentObservables to add additional functionality.

Here is a sample: http://jsfiddle.net/rniemeyer/ZP79w

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文