1

我有两个主要问题。首先,我在第 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);

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

4

1 回答 1

1

您忘记添加“这个”。在 pressure2_select 前面。这是固定的小提琴

    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);
于 2013-05-17T00:17:40.953 回答