7

次のことを行う必要があります。ユーザーがチェックボックスをオンにすると、いくつかの関数が呼び出されます。

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

そしてモデルでは:

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

これについては何も見つかりませんでした。ドキュメントはこちらです。

4

1 に答える 1

19

必要なのはclickバインディングです:

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

JSFiddle のデモ。

または、checkedバインディングを使用する場合は、プロパティの変更イベントでサブスクライブできます。

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

そしてあなたのビューモデルで:

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 のデモ。

于 2013-06-26T08:04:14.330 に答える