我使用该插件:
https://github.com/plentz/jquery-maskmoney格式化我的钱编辑器…
我试图在该编辑器中使用KnockoutJS,但是它不起作用.没有这个掩码都可以正常工作
我的代码测试很简单:
<input id="Price" data-bind="value: Price" type="text" name="Price">
Javascript到Mask输入
$("#Price").maskMoney({ symbol: 'R$',showSymbol: true,thousands: '.',decimal: ',',symbolStay: false });
和KnockoutJS
var viewmodel = function () { this.Price = ko.observable(); this.PriceFinal= ko.computed(function () { return this.Price() },this); }; ko.applyBindings(new viewmodel());
解决方法
你应该使用一个可写的计算的observable.
function Myviewmodel() { this.price = ko.observable(25.99); this.formattedPrice = ko.computed({ read: function () { return '$' + this.price().toFixed(2); },write: function (value) { // Strip out unwanted characters,parse as float,then write the raw data back to the underlying "price" observable value = parseFloat(value.replace(/[^\.\d]/g,"")); this.price(isNaN(value) ? 0 : value); // Write to underlying storage },owner: this }); } ko.applyBindings(new Myviewmodel());