@Artem Vyshniakov 的回答是正确的。但是,如果您正在寻找 IE polyfill 而不是替换支持它的浏览器中的 html5 数字输入,请尝试以下操作:
ko.bindingHandlers.spinner = {
init: function (element, valueAccessor, allBindingsAccessor) {
if (Modernizr.inputtypes.number) {
ko.bindingHandlers.value.init.apply(null, arguments);
} else {
//initialize spinner with some optional options
var options = allBindingsAccessor().spinnerOptions || {};
$(element).spinner(options);
//handle the field changing
$(element).on("spinstop", function () {
var observable = valueAccessor();
observable($(element).spinner("value"));
});
//handle disposal
ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
$(element).off("spinstop");
$(element).spinner("destroy");
});
}
},
update: function (element, valueAccessor, allBindingsAccessor) {
if (Modernizr.inputtypes.number) {
ko.bindingHandlers.value.update.apply(null, arguments);
} else {
var value = ko.utils.unwrapObservable(valueAccessor());
var disable = allBindingsAccessor().disable;
if (typeof disable !== "undefined") {
$(element).spinner((disable) ? "disable" : "enable");
}
var current = $(element).spinner("value");
if (value !== current) {
$(element).spinner("value", value);
}
}
}
};
function Order(name, price, vat, number) {
var self = this;
self.Name = ko.observable(name);
self.Price = ko.observable(price);
self.VAT = ko.observable(vat);
self.Number = ko.observable(number);
self.Final = ko.computed(function() {
return (self.Price() + self.VAT()) * self.Number();
});
}
function ViewModel() {
var self = this;
self.orders = ko.observableArray();
self.removeOrder = function(item) {
self.orders.remove(item);
};
self.save = function() {
alert(ko.toJSON(self));
};
}
var viewModel = new ViewModel();
viewModel.orders.push(new Order("Sugar", 100, 15, 3));
viewModel.orders.push(new Order("Salt", 200, 25, 4));
viewModel.orders.push(new Order("Milk", 200, 35, 1));
ko.applyBindings(viewModel);
在这里完成小提琴:http: //jsfiddle.net/mberkom/pCJWc/