0

我有一个输入字段,当Selectbox更改其值时,应动态填充该字段。因此,我将变量绑定到输入,并将另一个值绑定到选择框的选定值。现在我希望能够以编程方式更改输入值:对于选择框的某些值,该值应该是一个空字符串,而对于其他值,我应该在用户执行某些操作时使用一些 javascript 代码来设置它。I though to use a computed function, so I can return the empty string but this lead me to a problem: how can I then set the value programmatically when the other selectbox options are selected?

4

1 回答 1

1

input如果您的目标是在更改时默认值select并允许用户自由更新它,那么一个不错的选择是使用手动订阅

var ViewModel = function() {
   this.selectedValue = ko.observable();
   this.otherValue = ko.observable();

   this.selectedValue.subscribe(function(newValue) {
       //perform whatever logic that you need to determine how the other value should be populated based on the dropdown's current value (newValue)
       this.otherValue(calculatedValue);
   }, this);
};
于 2012-11-20T21:41:49.080 回答