Javascript attr binding-KnockoutJs中的Bind对象

Javascript attr binding-KnockoutJs中的Bind对象,javascript,knockout.js,Javascript,Knockout.js,我需要在value属性中绑定对象,因为KnockoutJs将值作为checkbox事件中的第一个参数发送。 当作为单个值传递时,一切正常 <div data-bind="foreach: Items"> <input type="checkbox" value data-bind="attr: { value: id }, checked: selectedVal"> this.selectedVal.subscribe((newVal: any) => {

我需要在value属性中绑定对象,因为KnockoutJs将值作为checkbox事件中的第一个参数发送。 当作为单个值传递时,一切正常

<div data-bind="foreach: Items">
   <input type="checkbox" value data-bind="attr: { value: id }, checked: selectedVal">

this.selectedVal.subscribe((newVal: any) => {
  alert(newVal);
});

在订阅时,我希望整个对象具有id和名称,请告诉我如何执行此操作?

您可以使用
checkedValue:$data
告诉复选框写入整个对象。例如:

const项=[
{
id:1,
名字:'亚当',
},
{
id:2,
姓名:'贝利',
},
{
id:3,
姓名:'凯西',
},
];
所选常数=ko.observearray([]);
selected.subscribe(console.log);
ko.applyBindings({items,selected})

Items = [
  {
    id: 1,
    name: 'Adam',
  },
  {
    id: 2,
    name: 'Bailey',
  },
  {
    id: 3,
    name: 'Cathy',
  },
]