JSFiddle - React, Tailwind, and code Playground
by kougiland
HTML
<input data-bind="checked: selectedValues, value:primaryClass" type="checkbox" >500</input>
<input data-bind="checked: selectedValues,value: secondaryClass" type="checkbox">200</input>
<input data-bind="checked: selectedValues,value:otherClass" type="checkbox">100</input>
<br/>
Result:
<input data-bind="value: selectedValues().length > 0 ? sum : alternativeValue" type="text"></input>
JavaScript
function ViewModel() {
var self = this;
self.primaryClass = ko.observable("50");
self.secondaryClass = ko.observable("40");
self.otherClass = ko.observable("10");
self.alternativeValue = ko.observable("200");
self.selectedValues = ko.observableArray([]);
self.sum = ko.computed(function () {
var total = 0;
ko.utils.arrayForEach(self.selectedValues(), function (item) {
console.log(item);
total += parseInt(item);
});
return total;
});
}
ko.applyBindings(new ViewModel());