我正在使用该插件:https://github.com/plentz/jquery-maskmoney格式化我的货币编辑器...
我试图在那个编辑器中使用 KnockoutJS,但它不起作用......没有那个面具一切正常......
我的代码测试很简单:
<input id="Price" data-bind="value: Price" type="text" name="Price">
Javascript 屏蔽输入
$("#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());
关于knockout.js - 是否可以将 KnockoutJS 与屏蔽输入一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9160446/