计算的Observable不更新

时间:2013-05-16 23:56:51

标签: javascript knockout.js

我有两个主要问题。首先,我在第16行的括号导致错误。其次,更重要的是,即使删除该行,数据绑定也不会更新为计算属性。这是一个jsFiddle:http://jsfiddle.net/austinfacts/Lxpgu/1/ 或者只是你喜欢的代码。

<form>
                        <p>
                            Known Volume: <input id="boyle1_volume1_text" data-bind="value: volume1_text" type="text" />
                        </p>
                        <p>
                            In: <select id="boyle1_volume1_select" data-bind="value: volume1_select">
                                    <option value="0">- Select -</option>
                                    <option value="1">Liters</option>
                                    <option value=".001">Milliliters</option>
                            </select>
                        </p>
                        <p>
                            Pressure Corresponding to Known Volume: <input id="boyle1_pressure1_text" data-bind="value: pressure1_text" type="text" />
                        </p>
                        <p>
                            In: <select id="boyle1_pressure1_select" data-bind="value: pressure1_select">
                                <option value="0">- Select -</option>
                                <option value="101.3">Atmospheres</option>
                                <option value="1">Kilopascals</option>
                            </select>
                        </p>
                        <p>
                            Known Pressure Corresponding to Unknown Volume: <input id="boyle1_pressure2_text" data-bind="value: pressure2_text" type="text" />
                        </p>
                        <p>
                            In: <select id="boyle1_pressure2_select" data-bind="value: pressure2_select">
                                <option value="0">- Select -</option>
                                <option value="101.3">Atmospheres</option>
                                <option value="1">Kilopascals</option>
                            </select>
                        </p>
                        <p>
                            Answer In: <select id="boyle1_volume2_select" data-bind="value: volume2_select">
                                    <option value="0">- Select -</option>
                                    <option value="1">Liters</option>
                                    <option value="1000">Milliliters</option>
                            </select>
                        </p>
                        <span id="boyle1_volume_result" data-bind="text: answer1"></span>

var boyle1 = {
volume1_text: ko.observable(parseInt(0)),
volume1_select: ko.observable(parseInt(0)),

pressure1_text: ko.observable(parseInt(0)),
pressure1_select: ko.observable(parseInt(0)),

pressure2_text: ko.observable(parseInt(0)),
pressure2_select: ko.observable(parseInt(0)),

volume2_select: ko.observable(parseInt(0)),
};
boyle1.answer1 = ko.computed(function () {
    return this.volume1_text() * this.volume1_select() 
    * this.pressure1_text() * this.pressure1_select()
    / ( this.pressure2_text() * pressure2_select() )
    * this.volume2_select();
}, boyle1);
ko.applyBindings(boyle1);

我做错了什么?任何输入都会有所帮助。

1 个答案:

答案 0 :(得分:1)

你忘记添加'这个'。在pressure2_select面前。这是固定的fiddle

    var boyle1 = {
        volume1_text: ko.observable(parseInt(0)),
        volume1_select: ko.observable(parseInt(0)),

        pressure1_text: ko.observable(parseInt(0)),
        pressure1_select: ko.observable(parseInt(0)),

        pressure2_text: ko.observable(parseInt(0)),
        pressure2_select: ko.observable(parseInt(0)),

        volume2_select: ko.observable(parseInt(0)),
    };

    boyle1.answer1 = ko.computed(function () {
        return this.volume1_text() * this.volume1_select() 
        * this.pressure1_text() * this.pressure1_select()
        / ( this.pressure2_text() * this.pressure2_select() )
        * this.volume2_select();
    }, boyle1);

    ko.applyBindings(boyle1);