gpt4 book ai didi

javascript - knockoutjs 勾选数据绑定(bind)调用函数

转载 作者:数据小太阳 更新时间:2023-10-29 05:43:27 24 4
gpt4 key购买 nike

我需要做以下事情:当用户选中复选框时,会调用一些函数。

<input type="checkbox" data-bind="what to write here?" />

在模型中:

var viewModel = {
this.someFunction = function() {
console.log("1");
}
};

我还没有找到任何有关此文档的信息 here .

最佳答案

您需要的是 click binding:

<input type="checkbox" data-bind="click: someFunction" />

在你的 View 模型中:

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
}
};

演示 JSFiddle.

或者,如果你想使用 checked 绑定(bind),你可以订阅属性的更改事件:

<input type="checkbox" data-bind="checked: isChecked" />

在你的 View 模型中:

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");
}
};

演示 JSFiddle.

关于javascript - knockoutjs 勾选数据绑定(bind)调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17314610/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com