2

我正在使用 JQuery Mobile 和 KnockoutJS 开发一个移动应用程序。能够从 AJAX 调用成功加载复选框列表。复选框列表已使用淘汰赛 js foreach 绑定进行绑定。但是我想不出一种方法来从 lsit 中获取选定的复选框值。任何想法都将是非常可观的。

这是我的 HTML

 <p>
    Select Items(s):
    <div id="itemList">
    <ul id="itemList" data-role="listview"  data-inset="true" >
        <fieldset id="myList" data-role="controlgroup" data-bind="foreach : Items">
            <li id="chkRoles" data-role="fieldcontain" data-filter="true">
               <input data-theme="c" type="checkbox" data-role="controlgroup" data-bind="attr: { 'data-id': Id, name: 'checkbox-' + Id, id: 'checkbox-' + Id  }" />
                <label data-theme="c" data-bind="text: ItemName, attr: { for: 'checkbox-' + Id }"></label>
            </li>
     </fieldset>
     </ul>
    </div>
</p>    

我的视图模型 JS

    ItemViewModel = function(){
var self = this;
self.Items = ko.observableArray([])
}

var viewModel = new ItemViewModel();

//doing an AJAX call to load ObservableArray
viewModel.Items(data);
$("#itemList").listview("refresh").find('input').checkboxradio();

ko.applyBindings(viewModel);
4

1 回答 1

1

You can have another observableArray to hold the checked values only. Then, if you give your checkboxes a 'value' attribute, and use the 'checked' binding on them, specifying the observableArray, the array should automatically be updated only with the values of the checked inputs. (This is all explained here)

I put your code into a jsfiddle and made those changes here: http://jsfiddle.net/antishok/ektUp/

于 2012-04-29T20:15:50.120 回答