在knockoutjs 中使用复选框列表 [英] Working with a list of checkboxes in knockoutjs

查看:16
本文介绍了在knockoutjs 中使用复选框列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解 Knockout.js,但在复选框方面我很困惑.

I'm trying to get my head around Knockout.js and I'm quite stuck when it comes to checkboxes.

服务器端我用它们对应的值填充一组复选框.现在,当检查任何未选中的复选框时,我需要将其存储在逗号分隔的字符串中.如果未选中它们,则需要从字符串中删除该值.

Server side I'm populating a set of checkboxes with their corresponding values. Now, when any of the unchecked checkboxes are checked, I need to store it's value in a comma-seperated string. When they're unchecked, the value needs to be deleted from the string.

有没有人知道如何使用knockoutjs 实现这一点?

Have anyone got a hint on how to achieve this with knockoutjs?

到目前为止,我有以下代码:

I have the following code so far:

视图模型:

$().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 循环:

And my checkboxes are populated with a foreach loop:

<input data-bind="checked: function() { editOfferViewModel.addPreValue(this) }"
       type="checkbox" checked="yes" value='@s'>
    @s
</input>

我尝试将复选框元素作为参数传递给我的 addPreValue() 函数,但是当我选中复选框时似乎没有任何反应?

I try to pass the checkbox element as the parameter to my addPreValue() function, but nothing seems to happen when I check the checkbox?

非常感谢您对此的任何帮助/提示!

Any help/hints on this is greatly appreciated!

推荐答案

被检查的绑定期望传递一个它可以读/写的结构.这可以是一个变量、一个 observable 或一个可写的dependentObservable.

The checked binding expects to be passed a structure that it can read/write against. This could be a variable, an observable, or a writable dependentObservable.

当传递一个数组或 observableArray 时,被检查的绑定确实知道如何从数组中添加和删除简单的值.

When passed an array or observableArray, the checked binding does know how to add and remove simple values from the array.

这是一个示例,其中还包括一个计算出的 observable,其中包含数组作为逗号分隔值.http://jsfiddle.net/rniemeyer/Jm2Mh/

Here is a sample that also includes a computed observable that contains the array as comma delimited values. http://jsfiddle.net/rniemeyer/Jm2Mh/

var viewModel = {
    choices: ["one", "two", "three", "four", "five"],
    selectedChoices: ko.observableArray(["two", "four"])
};

viewModel.selectedChoicesDelimited = ko.computed(function() {
    return this.selectedChoices().join(",");
}, viewModel);

ko.applyBindings(viewModel);

HTML:

<ul data-bind="template: { name: 'choiceTmpl', foreach: choices, templateOptions: { selections: selectedChoices } }"></ul>

<script id="choiceTmpl" type="text/html">
    <li>
        <input type="checkbox" data-bind="attr: { value: $data }, checked: $item.selections" />
        <span data-bind="text: $data"></span>
    </li>
</script>

这篇关于在knockoutjs 中使用复选框列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆