【发布时间】:2011-10-07 20:18:21
【问题描述】:
我正试图了解 Knockout.js,但在涉及复选框时我很困惑。
服务器端我正在使用它们对应的值填充一组复选框。现在,当检查任何未选中的复选框时,我需要将其存储在逗号分隔的字符串中。当它们未选中时,需要从字符串中删除该值。
有没有人知道如何使用 knockoutjs 实现这一点?
到目前为止,我有以下代码:
视图模型:
$().ready(function() {
function classPreValue(preValue)
{
return {
preValue : ko.observable(preValue)
}
}
var editOfferViewModel = {
maxNumOfVisitors : ko.observable(""),
goals : ko.observable(""),
description : ko.observable(""),
contact : ko.observable(""),
comments : ko.observable(""),
classPreValues : ko.observableArray([]),
addPreValue : function(element) {
alert($(element).val());
this.classPreValues.push(new classPreValue(element.val()));
}
};
ko.applyBindings(editOfferViewModel);
});
我的复选框填充了一个 foreach 循环:
<input data-bind="checked: function() { editOfferViewModel.addPreValue(this) }"
type="checkbox" checked="yes" value='@s'>
@s
</input>
我尝试将复选框元素作为参数传递给我的addPreValue() 函数,但是当我选中复选框时似乎没有任何反应?
非常感谢任何帮助/提示!
【问题讨论】:
标签: javascript html mvvm checkbox knockout.js