Warm tip: This article is reproduced from serverfault.com, please click

Phone Mask does not work before typing

发布于 2015-11-24 16:02:42

I have a cellphone bootstrap text box. After i click the textbox i can see the mask but i would like to see the mask as soon as i open the modal.

note:self.EStates stop woking after adding mask.

onload enter image description here

after clicking enter image description here

ko.extenders.mask = function (observable, mask) {
        observable.mask = mask;
        return observable;
    }


    var orgValueInit = ko.bindingHandlers.value.init;
    ko.bindingHandlers.value.init = function (element, valueAccessor) {
        var mask = valueAccessor().mask;
        if (mask) {
            $(element).mask(mask);
        }

        orgValueInit.apply(this, arguments);
    }

function ViewModel() {
self.cellPhone = ko.observable().extend({
            required: true,
            mask: "(999) 999-9999"
        });

  self.getCellPhoneNumberForAgent = function () {
            var cellPhone = "";
            var responseFromGetCellPhoneNumber = $.getJSON('GetCellPhoneNumberForAgent', function (cellPhoneResponse) {
                cellPhone = cellPhoneResponse;
            }).done(function () {
                var cellPhoneNumberForAgent = $.parseJSON(cellPhone);
                self.cellPhone(cellPhoneNumberForAgent);
            });
        };
        self.getCellPhoneNumberForAgent();


    //This is not working after adding mask code.
    self.EStates = ko.observableArray([]);
        $.getJSON('GetEStates', function (data) {
            var result = $.parseJSON(data);
            $.each(result, function (key, value) {
                var name = value;
                var state = new eligibleState(name, false);
                self.EStates.push(state);
            });
        });
}
Questioner
Nakres
Viewed
0
Roy J 2015-11-25 00:46:29

jQuery mask isn't really designed to handle programmatically updated values. You need to call unmask and then apply the mask again on every update.

ko.extenders.mask = function(observable, mask) {
  observable.mask = mask;
  return observable;
};

var origValueUpdate = ko.bindingHandlers.value.update;
ko.bindingHandlers.value.update = function(element, valueAccessor) {
  var val = valueAccessor(),
    mask = val.mask,
    newValue = val(); // Just so it knows when to update
  $(element).unmask();
  origValueUpdate.apply(this, arguments);
  if (mask)
    $(element).mask(mask);
};

function ViewModel() {
  var self = this;
  self.cellPhone = ko.observable().extend({
    required: true,
    mask: "(999) 999-9999"
  });

  self.cellPhone("1112223333");
}

var vm = new ViewModel();
ko.applyBindings(vm);

setTimeout(function() {
  vm.cellPhone("2223334444");
}, 1500);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.13.4/jquery.mask.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<input data-bind="value:cellPhone" />