knockout.js - Knockout radio button check binding -


i have viewmodel in have array of "answers". "answer" object has property 'selected'. in demo array have 2 answers. first selected=1 , second selected=0. have no idea why both radio buttons selected. here demo link - https://jsfiddle.net/jwoscjot/3/ , binding is

<input type="radio"  data-bind=" value: selected, checked: selected"> 

from http://knockoutjs.com/documentation/checked-binding.html

for radio buttons, ko set element checked if , if parameter value equals radio button node’s value attribute or value specified checkedvalue parameter.

you should consider adding selectedanswerid property view model.

<!-- ko foreach: answers -->     <input type="radio" data-bind="value: answerid, checked: $parent.selectedanswerid"> <!-- /ko --> 

if want keep answer's selected state part of answer, can used checkedvalue part of binding. note won't have way "deselect" answer unless provide sort of "clear" button because radio buttons aren't intended support multi-select.

<!-- ko foreach: answers -->     <input type="radio" data-bind="checked: selected, checkedvalue: 1"> <!-- /ko --> 

Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -