たぶんextendersあなたの仕事のためのより良いソリューションですか?
あなたが必要とするものの例があります。
ko.extenders.numeric = function(target, precision) {
//create a writeable computed observable to intercept writes to our observable
var result = ko.computed({
read: target, //always return the original observables value
write: function(newValue) {
var current = target(),
roundingMultiplier = Math.pow(10, precision),
newValueAsNum = isNaN(newValue) ? 0 : parseFloat(newValue),
valueToWrite = Math.round(newValueAsNum * roundingMultiplier)/roundingMultiplier;
//only write if it changed
if (valueToWrite !== current) {
target(valueToWrite);
} else {
//if the rounded value is the same, but a different value was written, force a notification for the current field
if (newValue != current) {
target.notifySubscribers(valueToWrite);
}
}
}
});
//initialize with current value to make sure it is rounded appropriately
result(target());
//return the new computed observable
return result;
};
function AppViewModel(one, two) {
this.myNumberOne = ko.observable(one).extend({ numeric: 0 });
this.myNumberTwo = ko.observable(two).extend({ numeric: 2 });
}
ko.applyBindings(new AppViewModel(221.2234, 123.4525));
私は入力を取得し、event.keyCodeが入り、値が一定量であれば、からkeyupイベントでそれにいくつかのCSSを適用したいです。私はまた、値を調べ、10進数を入力した場合はそれを切り捨てたい。 – NibblyPig