[英]Determine if the checkbox is checked on click event in Knockout.js
I have this checkbox control: 我有此复选框控件:
<input type="checkbox" data-bind="attr: { id: 'prefix_12', attributeid: 12, poseId: $parent.Id }, click: $root.addEffect" />
Add Option
And the event code: 和事件代码:
self.addEffect = function (c, event) {
var target = event.target;
}
What I want to know is how can I determine if the checkbox is checked or unchecked for each click event? 我想知道的是如何确定每个单击事件的复选框处于选中状态还是未选中状态?
The knockout way would be to bind checkbox against boolean property like this 淘汰赛的方法是将复选框绑定到像这样的布尔属性
<input type="checkbox" data-bind="checked: someBoolProperty" />
In viewmodel 在视图模型中
self.someBoolProperty = ko.observable();
And rather than use click event, subscribe to the property 而不是使用click事件,而是订阅属性
self.someBoolProperty.subscribe(function(newValue){
if (newValue){
// do whatever you want to do for checked checkbox
} else {
// unchecked
}
}, self);
You can use event.currentTarget
您可以使用event.currentTarget
if (event.currentTarget.checked) {
// do the stuff
}
else {
// do the stuff
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.