简体   繁体   English

淘汰赛计算的观察不到

[英]Knockout computed observable not working

I am using a Knockout computed observable to multiply two other observables. 我正在使用淘汰赛计算的可观察值乘以其他两个可观察值。 Unfortunately, the computed observable does not seem to be outputting any value. 不幸的是,计算出的可观察值似乎没有输出任何值。

var boyle1 = {
volume1_text: ko.observable(parseInt(2)),
volume1_select: ko.observable(parseInt(2))
};
boyle1.volume = ko.computed(function () {
    return this.volume1_text() * this.volume1_select();
}, boyle1);
ko.applyBindings(boyle1);

Both other observables work perfectly, and are easily binded to elements on the page. 其他两个可观察对象都可以正常工作,并且可以轻松绑定到页面上的元素。 What am I doing wrong? 我究竟做错了什么?

It has something to do with your html binding to the computed. 它与您的html绑定到计算对象有关。 As you indicated it was in fact, a typo in the markup. 正如您所指出的,实际上是标记中的错字。

Markup 标记

<input data-bind="value: volume1_text" />
<input data-bind="value: volume1_select" />
<br /><br />    
<span data-bind="text: volume"></span>

Model 模型

var boyle1 = {
   volume1_text: ko.observable(parseInt(2)),
   volume1_select: ko.observable(parseInt(2))
};

boyle1.volume = ko.computed(function () {
    return this.volume1_text() * this.volume1_select();
}, boyle1);

ko.applyBindings(boyle1);

Here is a working example of your model and problem 这是您的模型和问题的有效示例

http://jsfiddle.net/YBr2m/1 http://jsfiddle.net/YBr2m/1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM