如何在下拉淘汰赛js中预先选择一个选项
我已经查看了其他问题,但无法让我的选择框正常工作: 绑定下拉(选择)列表的初始/默认值< /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 是一个具有 Name
和 SportTypeId
的对象。
我有以下模板:
<td rowspan="3"><select data-bind="options: AvailableSportTypes, value: SportType, optionsText:'Name', optionsCaption: 'Choose...'" class="sportType"></select></td>
AvailableSportTypes
是SportType
的列表。
该列表在下拉列表中包含 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当传入
game.SportType
时,它需要是对game.AvailableSportTypes
中项目的引用,而不仅仅是看起来相同的对象。基本上两个对象不相等,除非它们实际上是对同一对象的引用。
因此,您需要调用函数来在数组中找到正确的对象并将其设置为可观察值的值。
并不是说它更好,而是在 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 thegame.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.
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