简体   繁体   English

可观察和计算无法正常工作

[英]observable and computed are not working properly Knockout

I have the following knockout code that implements the folowing role: 我有以下实现以下角色的基因剔除代码:

field1 + field2 -field3 = field4 field1 + field2 -field3 = field4

$(function () {
ko.applyBindings(new AppViewModel());
});

function AppViewModel() {
this.original = ko.observable(0);
this.improvements = ko.observable(0);
this.depreciation = ko.observable(0);

this.total= ko.computed(function () {
    var total= 0;
    total= this.original() + this.improvements() - this.depreciation();
    return total;
}, this);
}

But for some reason it's not working properly, this.original is always multiplied by 10. 但是由于某种原因它无法正常工作,因此此this.original值始终会乘以10。

for example: 例如:

1 + 1 - 1 = 10 1 +1-1 = 10

Any idea what can causes this? 知道是什么原因造成的吗?

This is my HTML: 这是我的HTML:

<div class="calc-form">
     <label>Original Purchase Price</label>
     <input type="text" id="original" data-bind="value: original" />

     <label>+ Improvements</label>
     <input type="text" id="improvements" data-bind="value: improvements" />

     <label>- Depreciation</label>
     <input type="text" id="depreciation" data-bind="value: depreciation" />

     <input type="button" class="calcbutton" value="Calculate" />
     <input type="button" class="calcbuttonreset" value="reset" />
     <p>= Total</p>
     <span data-bind="text: total"></span>
</div>

Remember that the value of input elements is always a string . 请记住, input元素的值始终是字符串 "1" + "1" - "1" is "11" - "1" is 10 because when either operand is a string, + is string concatenation, not addition; "1" + "1" - "1""11" - "1"10因为当任一操作数为字符串时, +为字符串连接,而不是加法; but - is always subtraction, so it coerces its operands to numbers. 但是-总是相减,因此它将其操作数强制为数字。

You need to parse them, via +x or parseFloat(x) or Number(x) or (if they're meant to be whole numbers) parseInt(x, 10) , etc.: 您需要通过+xparseFloat(x)Number(x)或(如果它们是整数) parseInt(x, 10)等来解析它们:

total = parseFloat(this.original())
        + parseFloat(this.improvements())
        - parseFloat(this.depreciation());

Example: 例:

 $(function() { ko.applyBindings(new AppViewModel()); }); function AppViewModel() { this.original = ko.observable(0); this.improvements = ko.observable(0); this.depreciation = ko.observable(0); this.total = ko.computed(function() { var total = 0; total = parseFloat(this.original()) + parseFloat(this.improvements()) - parseFloat(this.depreciation()); return total; }, this); } 
 <div class="calc-form"> <label>Original Purchase Price</label> <input type="text" id="original" data-bind="value: original" /> <label>+ Improvements</label> <input type="text" id="improvements" data-bind="value: improvements" /> <label>- Depreciation</label> <input type="text" id="depreciation" data-bind="value: depreciation" /> <input type="button" class="calcbutton" value="Calculate" /> <input type="button" class="calcbuttonreset" value="reset" /> <p>= Total</p> <span data-bind="text: total"></span> </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script> 

If you're going to be working with numeric inputs a lot, you might want to give yourself a specific binding for them: 如果要大量使用数字输入,则可能要给自己一个特定的绑定:

// "numValue" binding handler (just an example)
ko.bindingHandlers.numValue = {
  init: function(element, valueAccessor) {
    function numValueHandler() {
      valueAccessor()(parseFloat(this.value));
    }
    $(element).on("input change", numValueHandler)
              .val(ko.unwrap(valueAccessor()));
    ko.utils.domNodeDisposal.addDisposeCallback(element, function() {
      $(element).off("input change", numValueHandler);
    });
  },
  update: function(element, valueAccessor) {
    element.value = ko.unwrap(valueAccessor());
  }
};

Then: 然后:

<input type="text" id="original" data-bind="numValue: original" />
<!-- ---------------------------------------^^^^^^^^           -->

 // "numValue" binding handler (just an example) ko.bindingHandlers.numValue = { init: function(element, valueAccessor) { function numValueHandler() { valueAccessor()(parseFloat(this.value)); } $(element).on("input change", numValueHandler) .val(ko.unwrap(valueAccessor())); ko.utils.domNodeDisposal.addDisposeCallback(element, function() { $(element).off("input change", numValueHandler); }); }, update: function(element, valueAccessor) { element.value = ko.unwrap(valueAccessor()); } }; $(function() { ko.applyBindings(new AppViewModel()); }); function AppViewModel() { this.original = ko.observable(0); this.improvements = ko.observable(0); this.depreciation = ko.observable(0); this.total = ko.computed(function() { var total = 0; total = this.original() + this.improvements() - this.depreciation(); return total; }, this); } 
 <div class="calc-form"> <label>Original Purchase Price</label> <input type="text" id="original" data-bind="numValue: original" /> <label>+ Improvements</label> <input type="text" id="improvements" data-bind="numValue: improvements" /> <label>- Depreciation</label> <input type="text" id="depreciation" data-bind="numValue: depreciation" /> <input type="button" class="calcbutton" value="Calculate" /> <input type="button" class="calcbuttonreset" value="reset" /> <p>= Total</p> <span data-bind="text: total"></span> </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script> 

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

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