Knockout checkbox change event sends old value

后端 未结 3 1655
小鲜肉
小鲜肉 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

    I also had this issue, but i couldn't use the subscribe solution because i was already subscribed to the same field with a ajax request that could reset the value. The code would then stay in a loop when you changed it. So i added the following workaround (its ugly but it works).

    $("input[type='radio'], input[type='checkbox']", element).on("change", function (e, data) {
        setTimeout(function () {
            $(this).trigger("afterChange", data);
        }.bind(this), 10);
    }); 
    

    Then instead on listening to the change i would listen to the afterChange event.

    I know its not the best solution, but in my case i had no other option.

提交回复
热议问题