0

With knockoutjs and jquery mobile, I need to create a list of checkboxes from an array. It seems the checkbox list is rendered, but it did not respond to click. http://jsfiddle.net/9zx7F/

I used a fieldset tag with data-role of controlgroup to build the list. I tried ul with listview as well, same issue.

Edit: further details - I found it seems related to timing of ko.applyBindings happens. I created a page with same code running on my localhost, it was okay. Then added a timer around ko.applyBindings, the issue happened again. http://jsfiddle.net/gonglei/9zx7F/12/

0

3 Answers 3

1

I solved this with two steps;

1) unwrapping the label from the input and hooking them together with 'for' attribute

    <input type="checkbox" data-role="checkbox" data-bind="uniqueName: true, uniqueID: true, value: ID />
    <label data-bind="uniqueIDFor: true" >Click me</label>

    ko.bindingHandlers.uniqueIDFor = {
        init: function (element) {
            element.setAttribute("for", "ko_unique_" + ko.bindingHandlers.uniqueName.currentIndex);
        }
    };
    ko.bindingHandlers.uniqueID = {
        init: function (element) {
            element.setAttribute("id", "ko_unique_" + ko.bindingHandlers.uniqueName.currentIndex);
        }
    };

2) telling jqm to update the new content

    $('input:checkbox').trigger('create');
Sign up to request clarification or add additional context in comments.

Comments

0

I would change the model for this:

<!-- ko foreach: listItems-->
    <input type="checkbox" name="itemsList" value="name" />
    <span data-bind="text: name"></span>
<!-- /ko -->

the main thing to consider is the "value" property in the input control to render in the proper way.

Regards.

Comments

0

@tredder's solution works! Here's a fork of your fiddle using the attr data-bind attribute to bind the label, which to me looks cleaner: http://jsfiddle.net/aib42/AnKR6/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.