0

所以我在一个页面上说 5 个选择列表。我对它们中的每一个都有绑定选项,它们都绑定到同一个数据集,例如状态模型。

一旦有人更改其中一个选择的状态,我想对其采取行动。

有任何想法吗?

谢谢!

4

2 回答 2

1

通常,当值更改时,您使用 ako.computed来处理事情:http: //knockoutjs.com/documentation/computedObservables.html

但是如果你想观察一个特定的 observable 何时发生变化,你可以使用subscribe: http: //knockoutjs.com/documentation/observables.html

如果遇到问题,您可以分叉并更新此 jsfiddle 以向我发送代码示例:http: //jsfiddle.net/JasonMore/p6Vcc/

于 2012-04-30T15:15:34.273 回答
0

If I understood your question correctly, I imagine you have one ko.observable variable per select list that keeps track of the currently selected value. If you want to take action everytime one of these index changes, then you can use the subscribe function:

var viewModel = {
   firstSelectListIndex : ko.observable(), // bound to the first select list value
   secondSelectListIndex : ko.observable(),
   ...   
};

then if you are interested in taking action when the user changes the state of the first select list, you can do:

viewModel.firstSelectListIndex.subscribe(function(newValue) { 
    // your code
});

and you have the new value, associated to the new selected item in the list, passed as a parameter.

于 2012-05-01T11:39:35.673 回答