简体   繁体   English

单击时全部选中了剔除中的复选框数组

[英]checkbox array in knockout being all checked on click

I am having an issue with knockout, when i check a box, they're all being checked... 我在淘汰赛中遇到问题,当我选中一个框时,它们都被选中了...

this is what I have: _Categories_List has all the items, and My_categories is the empty list where I want to have each id added 这就是我所拥有的:_Categories_List具有所有项目,而My_categories是我想要添加每个ID的空列表

this is the code: 这是代码:

 <!-- ko foreach: _Categories_List --> 
    <input type="checkbox" data-bind="attr: {value: $data}, checked: $root.My_categories" />
    <span data-bind="text: CODE"></span><br />
 <!-- /ko -->

and this is the JS part of the code (i cant really change this as to how the code is in the documentation because i'm building off someone else's work and should use the same code - referring to the mapping.fromJS): 这是代码的JS部分(我无法真正更改文档中代码的方式,因为我正在构建其他人的工作,并且应该使用相同的代码-引用mapping.fromJS):

var Poi = new Object();  
Poi.My_categories = [];
var _Poi = ko.mapping.fromJS(Poi);

var Categories_List = [];
var _Categories_List = ko.mapping.fromJS(Categories_List);

$(document).ready
(
    function () {        
       ko.applyBindings(_Poi);
       // here there's an ajax function to load the categories returned in i_Input.My_Result, then:
       ko.mapping.fromJS(i_Input.My_Result, _Categories_List);
    }
);

this is what the object loaded from ajax looks like: 这是从ajax加载的对象的样子:

{"My_Result":[
  {"CODE":"chalet","DEF_POIS_CATEGORY_ID":2,"DESCRIPTION":"chalet","ENTRY_DATE":"2012-10-10","ENTRY_USER_ID":2,"OWNER_ID":1},
  {"CODE":"vila","DEF_POIS_CATEGORY_ID":3,"DESCRIPTION":"villa","ENTRY_DATE":"2012-10-10","ENTRY_USER_ID":2,"OWNER_ID":1}
]}

The checked binding works off of the value of the input element. checked绑定不使用输入元素的value In your code, you are setting the value equal to an object, which turns into [object Object] , so both of your inputs have the same value, which is why checking one toggles both. 在您的代码中,您将值设置为等于一个对象,该对象变成[object Object] ,因此您的两个输入都具有相同的值,这就是为什么选中一个将两者都切换的原因。

So, you would want to set your value equal to a key on the object like your CODE property. 因此,您需要将值设置为等于CODE属性之类的对象上的键。 If necessary, then you can use a computed observable to represent the actual objects. 如有必要,则可以使用计算出的可观察值来表示实际对象。

Here is a sample: http://jsfiddle.net/rniemeyer/7n9gR/ 这是一个示例: http : //jsfiddle.net/rniemeyer/7n9gR/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM