KnockoutJS绑定与输入掩码混乱

编程入门 行业动态 更新时间:2024-10-11 19:20:53
本文介绍了KnockoutJS绑定与输入掩码混乱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试将输入掩码应用于电话领域.在我应用KnockoutJS绑定之前,它一直有效,该绑定将删除蒙版,直到该字段获得焦点为止.

不起作用: jsfiddle/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 = koputed(function() { return this.firstName() + " " + this.lastName(); }, this); this.firstName('John'); this.lastName('Doe'); this.phone('1231231234'); }; ko.applyBindings(new ViewModel());

作品: jsfiddle/gxhjn/

var ViewModel = function(first, last) { this.firstName = ko.observable(first); this.lastName = ko.observable(last); this.fullName = koputed(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 }); })

解决方案

我不认为这是一个淘汰赛问题,但与屏蔽输入插件的设计方式有关的问题:您的初始值必须与屏蔽条件匹配.即使您摆脱淘汰赛,仅使用jQuery的val()函数将值设置为"1231231234",您也会看到相同的行为.

更新

抱歉,错过了您的作品"链接.我首先会建议一个自定义绑定处理程序.看起来这是要走的路.它会在通过敲除来更新文本后,在文本后加上 遮罩,然后使用新的遮罩值(如果需要的话)来更新视图模型:

ko.bindingHandlers.maskedInput = { init: function(element, valueAccessor, allBindings, viewModel, bindingContext) { ko.bindingHandlers.value.init(element, valueAccessor, allBindings, viewModel, bindingContext); }, update: function(element, valueAccessor, allBindings, viewModel, bindingContext) { ko.bindingHandlers.value.update(element, valueAccessor, allBindings, viewModel, bindingContext); $(element).mask(allBindings.get('mask')); valueAccessor()($(element).val()); } };

这是您更新的小提琴: jsfiddle/8r6fe/3/

I'm trying to apply an input mask to a telephone field. It works until I apply KnockoutJS binding, which removes the mask until the field receives focus.

Does not work: jsfiddle/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 = koputed(function() { return this.firstName() + " " + this.lastName(); }, this); this.firstName('John'); this.lastName('Doe'); this.phone('1231231234'); }; ko.applyBindings(new ViewModel());

Works: jsfiddle/gxhjn/

var ViewModel = function(first, last) { this.firstName = ko.observable(first); this.lastName = ko.observable(last); this.fullName = koputed(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 }); })

解决方案

I don't think this is a knockout issue, but an issue with the way the masked input plugin is designed: Your initial value must match the mask criteria. Even if you get rid of knockout and just use jQuery's val() function to set the value to "1231231234", you'll see the same behavior.

UPDATE

Sorry, missed your "works" link. I was going to suggest a custom binding handler in the first place. Looks like this is the way to go. It applies the masking after the text has been updated by knockout, and then updates the view model with the new, masked value (if that's what you want):

ko.bindingHandlers.maskedInput = { init: function(element, valueAccessor, allBindings, viewModel, bindingContext) { ko.bindingHandlers.value.init(element, valueAccessor, allBindings, viewModel, bindingContext); }, update: function(element, valueAccessor, allBindings, viewModel, bindingContext) { ko.bindingHandlers.value.update(element, valueAccessor, allBindings, viewModel, bindingContext); $(element).mask(allBindings.get('mask')); valueAccessor()($(element).val()); } };

Here's your updated fiddle: jsfiddle/8r6fe/3/

更多推荐

KnockoutJS绑定与输入掩码混乱

本文发布于:2023-11-13 04:25:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1583422.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   掩码   混乱   KnockoutJS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!