Javascript 与收音机盒绑定的敲除装置

Javascript 与收音机盒绑定的敲除装置,javascript,jquery,html,knockout.js,Javascript,Jquery,Html,Knockout.js,我有收音机。为此,我绑定了单击事件。 问题:如果从一开始就要检查这个收音机盒,它不是 所以我想添加checked事件。但这将阻止单击逻辑,因此看起来像双击 创建ViewModel时,如何将所有事件正确绑定到收音机,而无需手动调用单击 <input data-bind="css: { checked: $component.isActiveGroup(value) }, click: $component.setActiveGrou

我有收音机。为此,我绑定了
单击
事件。
问题:如果从一开始就要检查这个收音机盒,它不是

所以我想添加
checked
事件。但这将阻止
单击
逻辑,因此看起来像双击

创建ViewModel时,如何将所有事件正确绑定到收音机,而无需手动调用
单击

<input data-bind="css: { checked: $component.isActiveGroup(value) },
                              click: $component.setActiveGroup.bind($component, value), 
                              checked: $component.activeGroup"
                   value={{value}}
                   type="radio" />

setActiveGroup(groupName: string) {
    if (!groupName) {
        return;
    }
    const isActive = this.isActiveGroup(groupName);
    this.activeGroup(isActive ? '' : groupName);
}

isActiveGroup(groupName: string) {
    return this.activeGroup() === groupName;
}

activeGroup = ko.observable('');

setActiveGroup(组名:字符串){
如果(!groupName){
返回;
}
const isActive=this.isActiveGroup(groupName);
this.activeGroup(isActive?“”:groupName);
}
isActiveGroup(组名:字符串){
返回此.activeGroup()==groupName;
}
活性组=可观察到的ko(“”);

选中的
绑定在默认情况下可以满足您的所有需要:

var VM=function(){
this.activeGroup=ko.可观察(“A”);
this.activeGroup.subscribe(console.log.bind(console));
this.isActiveGroup=groupName=>{
返回此.activeGroup()==groupName;
}
组=[“A”、“B”、“C”];
}
应用绑定(新VM())
。选中{背景:黄色;}

已选中的
绑定计算结果为true时,将选中该复选框。就这么简单。在应用程序启动时创建一个可观察(!)值,该值的计算结果为
true
,并将复选框绑定到该值。删除选中或取消选中复选框的任何其他代码。