我需要做以下事情:当用户选中复选框时,会调用某个函数。
<input type="checkbox" data-bind="what to write here?" />
并在模型中:
var viewModel = {
this.someFunction = function() {
console.log("1");
}
};
我还没有找到任何关于这个文档here。
答案 0 :(得分:15)
您需要的是click
binding:
<input type="checkbox" data-bind="click: someFunction" />
在您的视图模型中:
var ViewModel = function(data, event) {
this.someFunction = function() {
console.log(event.target.checked); // log out the current state
console.log("1");
return true; // to trigger the browser default behavior
}
};
或者,如果您想使用checked
绑定,可以订阅您房产的更改事件:
<input type="checkbox" data-bind="checked: isChecked" />
在你的viewmodel中:
var ViewModel = function() {
this.isChecked = ko.observable();
this.isChecked.subscribe(function(newValue){
this.someFunction(newValue);
}, this);
this.someFunction = function(value) {
console.log(value); // log out the current state
console.log("1");
}
};