Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem is that your value is an integer, but when bound to the <code>checkbox</code> element, it becomes a string. When the <code>checked</code> binding tries to find the value in the array, it doesn't find a match because it uses strict equality for comparison and <code>(2 === "2")</code> is false.</p> <p>The simplest way to work around this problem is to convert your values to string when you add them to the array:</p> <pre><code>self.selectedShifts.push("" + shift.ID); </code></pre> <p>Of course this means that your model has to change, and that might not be a great solution. I came up with a custom binding, <code>checkedInArray</code> that replaces <code>checked</code> and supports any type of value. You can <a href="https://stackoverflow.com/a/14863924/1287183">learn about it</a>, <a href="http://jsfiddle.net/mbest/L3LeD/" rel="nofollow noreferrer">see it in action</a>, and use it like this: </p> <pre><code>&lt;input type="checkbox" data-bind="checkedInArray: {value: ID, array: $root.selectedShifts }" /&gt; </code></pre> <p>In Knockout 2.3.0 (which is still in development) there will be a new binding, <a href="https://github.com/SteveSanderson/knockout/pull/875" rel="nofollow noreferrer"><code>checkedValue</code></a>, that will allow you use any type of value with the <code>checked</code> binding. Using that version, you could update your HTML to use <code>checkedValue</code>:</p> <pre><code>&lt;input type="checkbox" data-bind="checkedValue: ID, checked: $root.selectedShifts"/&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload