Hi please check my code belos
<dl data-bind="foreach: MobileNos" class="morenumbeList">
<dd><a data-bind="click: $parent.delete" class="minus">-</a><span class="number" data-bind="text: mobileno"></span>
<input id="itemID" data-bind="checked: $parent.SelectPrimary,attr : { id : 'Primary_' + $index() + ''} " type="radio" name="primary"/></dd>
</dl>
My script :
var mobilenoList = function (mobileno, primary) {
this.mobileno = mobileno;
this.primary = primary;
};
var MobileSave = function (mobileno, primary) {
this.mobileno = mobileno;
this.primary = primary;
}
function AppViewModel() {
var self = this;
self.MobileNos = ko.observableArray();
self.mobileno = ko.observable("");
self.primary = ko.observable(false);
self.itemID = ko.observable(0);
self.SelectPrimary = ko.computed(function () {
var mobnums = self.MobileNos();
$.each(self.MobileNos(), function (index, mobile) {
if (mobnums[index].primary) {
return true;
}
else {
return false;
}
});
})
self.MobileNos.subscribe(function () {
var mobnums = self.MobileNos();
for (var i = 0, j = mobnums.length; i < j; i++) {
var MobileNo = mobnums[i];
if (!MobileNo.index) {
MobileNo.index = ko.observable(i + 1);
} else {
MobileNo.index(i + 1);
}
}
});
$.getJSON("/User/GetContactDetails/", { UserID: $('#UserId').val() }, function (MobileNos) {
$.each(MobileNos.rows, function (index, mobnum) {
self.MobileNos.push(new mobilenoList(mobnum.mobileno, mobnum.primary));
})
});
};
$(document).ready(function () {
ko.applyBindings(new AppViewModel());
});
while I am loading page it should be select radio button , here I can get proper value in "self.MobileNos" but not able to check radio button according that
EDIT
<input id="itemID" value="false" data-bind="checked: $parent.SelectPrimary,attr : { id : 'Primary_' + $index() + ''} " type="radio" name="primary"/></dd>
self.SelectPrimary = ko.computed(function () {
var mobnums = self.MobileNos();
$.each(self.MobileNos(), function (index, mobile) {
if (mobnums[index].primary == true) {
return "true";
}
else {
return "false";
}
});
})