check jsfiddle
<div data-bind="foreach: ids" class="">
<label>
<input type="radio" name="someid" data-bind="value: value, checked: $root.selectedid" />
<span data-bind="text: text"></span>
</label>
</div>
<br/>
<input type="button" data-bind="click: test1" value="Test1"/>
<input type="button" data-bind="click: test2" value="Test2"/>
<input type="button" data-bind="click: test3" value="Test3"/>
radiodata function (text, value) {this.text = text; this.value = value; }
var viewModel = function(){
var self = this;
self.selectedid = ko.observable();
self.ids = ko.observableArray([
new radiodata('one',1),
new radiodata('two',2),
new radiodata('three',3)
]);
self.test1 = function(){
self.selectedid(2);
};
self.test2 = function(){
self.selectedid('2');
};
self.test3 = function(){
self.selectedid((3).toString());
};
}
var model = new viewModel();
ko.applyBindings(model);
I tried radio databindfor a property checked, My json data for selectedidare integers that I tried to bind checkedfor a radio.
I have a fixed solution for this, but does anyone know why the integer databind is not working?
like where self.testXfunctions will change the radio propertychecked
self.test1 = function(){
self.selectedid(2);
};
self.test2 = function(){
self.selectedid('2');
};
self.test3 = function(){
self.selectedid((3).toString());
};
any good explanation?
source
share