我正在尝试将输入掩码应用于电话字段。它一直有效,直到我应用 KnockoutJS 绑定,这会删除掩码,直到字段获得焦点。
不起作用:http: //jsfiddle.net/8r6fe/
$('[data-mask]').each(function () {
console.log('mask');
$this = $(this);
var mask = $this.attr('data-mask') || 'error...', mask_placeholder = $this.attr('data-mask-placeholder') || 'X';
$this.mask(mask, {
placeholder: mask_placeholder
});
})
var ViewModel = function() {
this.firstName = ko.observable("");
this.lastName = ko.observable("");
this.phone = ko.observable("");
this.fullName = ko.computed(function() {
return this.firstName() + " " + this.lastName();
}, this);
this.firstName('John');
this.lastName('Doe');
this.phone('1231231234');
};
ko.applyBindings(new ViewModel());
作品:http: //jsfiddle.net/gxhjn/
var ViewModel = function(first, last) {
this.firstName = ko.observable(first);
this.lastName = ko.observable(last);
this.fullName = ko.computed(function() {
// Knockout tracks dependencies automatically. It knows that fullName depends on firstName and lastName, because these get called when evaluating fullName.
return this.firstName() + " " + this.lastName();
}, this);
};
ko.applyBindings(new ViewModel("Planet", "Earth")); // This makes Knockout get to work
$('[data-mask]').each(function () {
console.log('mask');
$this = $(this);
var mask = $this.attr('data-mask') || 'error...', mask_placeholder = $this.attr('data-mask-placeholder') || 'X';
$this.mask(mask, {
placeholder: mask_placeholder
});
})