knockout.js - 是否可以将 KnockoutJS 与屏蔽输入一起使用?

标签 knockout.js maskedtextbox

我正在使用该插件: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/

相关文章:

javascript - Python Selenium 屏蔽字段

javascript - Knockout 绑定(bind)使 html(表格单元格)膨胀,如何使用 javascript 创建绑定(bind)或从父元素绑定(bind)?

javascript - 具有动态宽度的元素的动态数量,直到两行被填充

knockout.js - 按钮单击功能的数据绑定(bind)给出了 knockout 错误

knockout.js - 在 knockout 中选择后无法清除自动完成值

c# - 将2次之间的差异转换为毫秒?

c# - Maskedtextbox 掩码根据操作系统语言而变化

javascript - knockout 计算写入未触发

WPF MaskedTextBox 值绑定(bind)(无掩码绑定(bind))

javascript - 更改键入值的文本颜色,而不更改已存在值的颜色。使用 css3 或 javascript 或 jquery