Knockoutjs.com - Control types example
http://knockoutjs.com/examples/controlTypes.html
HTML
<script src="http://github.com/downloads/SteveSanderson/knockout/knockout-2.1.0.js"></script>
<div class='liveExample'>
<h3>Radio View model</h3>
<table>
<tr>
<td class="label">Radio buttons:</td>
<td>
<label><input name="Test" type="radio" value="A" data-bind="checked: radioSelectedOptionValue" />Alpha</label>
<label><input name="Test" type="radio" value="B" data-bind="checked: radioSelectedOptionValue" />Beta</label>
</td>
</tr>
</table>
A-<span data-bind="text: A"></span>
B-<span data-bind="text: B"></span>
</div>
JavaScript
var viewModel = function() {
this.radioSelectedOptionValue = ko.observable();
this.A = ko.computed(
{
read: function() {
return this.radioSelectedOptionValue() == "A";
},
write: function(value){
if (value)
this.radioSelectedOptionValue("A");
}
}
,this);
this.B = ko.computed(
{
read: function() {
return this.radioSelectedOptionValue() == "B";
},
write: function(value){
if (value)
this.radioSelectedOptionValue("B");
}
}
,this);
};
ko.applyBindings(new viewModel());