Knockout checkbox change event sends old value

后端 未结 3 1665
小鲜肉
小鲜肉 2021-02-01 15:10

I\'m having a problem with knockout \"checked\" binding. It seems that \"change\" event at checkbox return old value, before it is updated(so if it was unchecked it will return

3条回答
  •  星月不相逢
    2021-02-01 15:31

    Since you persisted on stating that the lack of ko.observables is not an issue, I've looked at it closer. It seems, that you are correct! The change event is fired before the actual value is set. I'm afraid I do not know the reason for this.

    But there is an easy way to fix this: just change change event to click event:

    
    

    Remember, that you have to explicitly put return true; at the end of click event handler. Otherwise the new value won't be set to checkbox.

    If you do not want to use click event, then you can do it the other way. Subscribe to changes of ShowOpened:

    this.ShowOpened = ko.observable(ShowOpened);
    this.ShowOpened.subscribe(function(newValue) {
        /* Do something when ShowOpened changes.
           newValue variable holds the new value, obviously. :) */
    });
    

提交回复
热议问题