0
我試圖使用knockoutjs綁定checkboxlist。 我有一個用戶相關的配置文件列表,我想要的是當頁面加載到初始化用戶配置文件的複選框列表。使用knockoutjs綁定checkboxlist
這是我的代碼
var userProfileList = [];
var jsonUserAdminModel = null;
var viewModel = {
,firstName: ko.observable()
,lastName: ko.observable()
,userId: ko.observable()
,userProfiles: ko.observableArray(userProfileList)
,result: ko.observable()
};
viewModel.result = ko.computed(function() {
jsonUserAdminModel = {
UserId: (this.userId() != undefined ? this.userId() : null)
,FirstName: (this.firstName() != undefined ? this.firstName() : null)
,LastName: (this.lastName() != undefined ? this.lastName() : null)
}
return jsonUserAdminModel;
}, viewModel);
$(document).ready(function() {
ko.applyBindings(viewModel);
//Init the list of all profiles
viewModel.userProfiles(@Html.Raw(Json.Encode(Model.Profiles)));
});
HTML
<div id="UserProfiles" data-bind="foreach: userProfiles" style="margin: 10px">
<input type="checkbox" data-bind="value: ProfileId" /><span data-bind="text: Name"></span>
</div>
我要檢查用戶的默認配置文件,並將其綁定。
謝謝。
不能確定你是問這裏。 – Dismissile
請爲此分享jsFiddle,這將很容易診斷問題。 – Rups