Using "checked" binding with bolean problem...

1,491 views
Skip to first unread message

Paul

unread,
Mar 18, 2011, 5:00:51 PM3/18/11
to KnockoutJS
Hello guys...

I have a bolean property called IsActive..
My model was created using Mapping plugin..

My Radio Html

<input type="radio" name="IsActive" value="true" data-bind="checked:
IsActive" />Yes
<input type="radio" name="IsActive" value='false' data-bind="checked:
IsActive" />No

Thats not working, the model always have the right value (true,
false), but it´s not binding propertly in UI...

Any idea

Thanks

rpn

unread,
Mar 18, 2011, 5:46:12 PM3/18/11
to knock...@googlegroups.com
Currently for radio buttons, it will use the value as a string.  So, when you check the radio buttons your observable is getting set to the string "true" or "false". 

If you were to do:

var viewModel = {
  IsActive: ko.observable("false")  //instead of ko.observable(false)
}

it would work or you could set IsActive after the fact like IsActive(IsActive().toString()).  You could even create a dependentObservable that return the .toString of IsActive and bind your inputs to it.

Otherwise, if you use a single checkbox instead of radio button for both options it would work with the boolean.

Does one of these options work for you?

Paul

unread,
Mar 18, 2011, 5:53:21 PM3/18/11
to KnockoutJS
Great!
Now I understand what is happening...

Thanks

Tom Thorp

unread,
Aug 11, 2011, 10:14:33 AM8/11/11
to knock...@googlegroups.com
I'm having the same issue, but I'm unable to use strings, so I modified lines 1679 to 1686 for knockout-1.2.1.debug.js

from
        } else if (element.type == "radio") {
            element.checked = (element.value == value);
           
            // Workaround for IE 6/7 bug - it fails to apply checked state to dynamically-created radio buttons if you merely say "element.checked = true"
            if ((element.value == value) && (ko.utils.isIe6 || ko.utils.isIe7))
                element.mergeAttributes(document.createElement("<input type='radio' checked='checked' />"), false);
        }



to
            } else if (element.type == "radio") {
                var testValue = value.toString();
                element.checked = (element.value === testValue);

                // Workaround for IE 6/7 bug - it fails to apply checked state to dynamically-created radio buttons if you merely say "element.checked = true"
                if ((element.value == testValue) && (ko.utils.isIe6 || ko.utils.isIe7))
                    element.mergeAttributes(document.createElement("<input type='radio' checked='checked' />"), false);
            }

I'm not sure that this is the "best" solution, but it seems to have fixed the problem with Boolean values. 

Any Thoughts?

Tom Thorp

unread,
Aug 11, 2011, 10:33:37 AM8/11/11
to knock...@googlegroups.com
Oops on previous post, forgot about null.  Below is fixed code.  My reason for the null is there are some cases where I have a nullable Boolean value in my MVC3 application.  What I want the user to do is to have to pick between two radio buttons.  By doing this, I can display a binding which results in neither of the two radio buttons being checked.

            } else if (element.type == "radio") {
                if (null === value)
                    testValue = null;
                else
Reply all
Reply to author
Forward
0 new messages