击倒计算参数未更新

Knockout Computed with Parameter not updated

本文关键字:更新 参数 计算      更新时间:2023-09-26

在MVC应用程序中,我从数据库生成多个下拉列表:

<select data-bind="options: findGroup(1).items(),
                        optionsText: 'country',
                        optionsValue: 'id',
                        value: selectedItem(1),
                        event: { change: selectionChange }"></select>

我需要在我的代码中选择当前,但为了调试目的,我使用span:

<span data-bind="text: 'Computed Selected Country: ' + selectedItem(1).country"></span><br />

findgroup(x)和selectedItem(x)是我的ViewModel中的全局函数,而它们对于所有的下拉菜单都是相同的。

selectedItem(x)应该返回当前选择的下拉菜单选项。selectedItem(x)是一个返回计算淘汰观察对象的函数。selectedItem(x)总是返回"undefined",不知道为什么…

完整示例:http://jsfiddle.net/LGveR/17/

TIA,保罗

你的函数返回一个计算值,所以它需要像这样执行

http://jsfiddle.net/LGveR/18/

this.selectedItem(1)().country