【发布时间】:2013-03-20 22:45:16
【问题描述】:
我有以下可观察数组:
self.Profiles =ko.observableArray( ko.utils.arrayMap(initialData, function (profile) {
return {
StartDate : formatDateOnly(profile.StartDate),
EndDate : formatDateOnly(profile.EndDate),
ProfileID :profile.ID,
ProfileName : profile.Name,
ProjectName : profile.ProjectName,
ReadingListID : profile.ReadingListID,
ReadingListName : profile.ReadingListName
};
}));
我想将一个下拉列表绑定到配置文件以显示配置文件名称,如果下拉列表的值发生变化,那么我想用所选 profileID 的新对应值更新 span 元素。
<table id="readingListApplyToProfile" class="fullWidthTable">
<tr>
<td>
Profile:
</td>
<td>
<select id="cboProfile" name="cboProfile" data-bind="options: Profiles, optionsText: 'ProfileName', 'optionsCaption': 'Select profile...', optionsValue:'ProfileID'"></select>
</td>
</tr>
<tr>
<td>
End Date:
</td>
<td>
<span data-bind="'text':EndDate"></span>
</td>
</tr>
</table>
我无法更新跨度,因为跨度元素不知道下拉列表的值,谁能帮助我。我完全迷路了。
【问题讨论】:
-
也许将 select 的值绑定到接受 ProfileID 并输出正确 Profile 的 ProfileName 的 computedObservable 上,然后将 spans 的文本绑定到相同的 computedObservable 上会起作用
-
这可能会起作用,但我不想为所有属性执行相同的代码,我希望有人能给我一个更好的解决方案。谢谢
-
有道理。这是一个完整的猜测,因为我不太擅长 computedObservables,但是否可以输出整个 Profile 对象,并绑定到 span 上的特定属性?喜欢
data-bind="text: getProfile.ProfileName"或类似的东西? -
你有多少这样的下拉菜单?这只是一个下拉菜单和一个跨度吗?
-
是的,它只有一个下拉菜单
标签: knockout.js