增量击倒可观察

时间:2014-03-27 07:03:23

标签: javascript knockout.js

我试图用1增加可观察变量,但它无法正常处理

JavaScript:

    self.fiscalYear = ko.observable(0);
    self.fiscalStartYear = ko.observable(0);
    self.fiscalEndYear = ko.observable(0);

    self.showYearsDetail = function () {
        self.fiscalStartYear(self.fiscalYear());
        self.fiscalEndYear(self.fiscalYear() + 1);
    };

    self.fiscalYear.subscribe(function () {
        self.showYearsDetail();
    });

HTML:

 <div class="row-fluid">
        <div class="span2 display-label">Fiscal Year  </div>
        <input id="fiscalYear" type="text" data-bind="value: fiscalYear" placeholder="Fiscal Year">
    </div>
    <div class="row-fluid">
        <div class="span2 display-label">Start Year  </div>
        <div class="span6 text-info"><span data-bind="text: fiscalStartYear"></span></div>
    </div>
    <div class="row-fluid">
        <div class="span2 display-label">End Year  </div>
        <div class="span6 text-info"><span data-bind="text: fiscalEndYear"></span></div>
    </div>

如果我插入2014

输出开始年份:2014年        结束年份:20145

1 个答案:

答案 0 :(得分:5)

尝试替换它:

self.fiscalEndYear(self.fiscalYear() + 1);

用这个:

self.fiscalEndYear(parseInt(self.fiscalYear()) + 1);